onion-wownero-blockchain-ex.../src/tools.cpp

1268 lines
28 KiB
C++
Raw Normal View History

2016-04-06 06:53:37 +00:00
//
// Created by marcin on 5/11/15.
//
#include "tools.h"
2017-02-02 14:17:43 +00:00
#include <codecvt>
#include <thread>
2016-04-06 06:53:37 +00:00
namespace xmreg
{
/**
* Parse key string, e.g., a viewkey in a string
* into crypto::secret_key or crypto::public_key
* depending on the template argument.
*/
2017-12-17 03:12:52 +00:00
template <typename T>
bool
parse_str_secret_key(const string& key_str, T& secret_key)
{
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
// hash and keys have same structure, so to parse string of
// a key, e.g., a view key, we can first parse it into the hash
// object using parse_hash256 function, and then copy the reslting
// hash data into secret key.
crypto::hash hash_;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
if(!parse_hash256(key_str, hash_))
{
cerr << "Cant parse a key (e.g. viewkey): " << key_str << endl;
return false;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
// crypto::hash and crypto::secret_key have basicly same
// structure. They both keep they key/hash as c-style char array
// of fixed size. Thus we can just copy data from hash
// to key
copy(begin(hash_.data), end(hash_.data), secret_key.data);
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
return true;
}
2016-04-06 06:53:37 +00:00
// explicit instantiations of get template function
2017-12-17 03:12:52 +00:00
template bool parse_str_secret_key<crypto::secret_key>(const string& key_str, crypto::secret_key& secret_key);
template bool parse_str_secret_key<crypto::public_key>(const string& key_str, crypto::public_key& secret_key);
template bool parse_str_secret_key<crypto::hash>(const string& key_str, crypto::hash& secret_key);
/**
2017-12-17 03:12:52 +00:00
* Get transaction tx using given tx hash. Hash is represent as string here,
* so before we can tap into the blockchain, we need to pare it into
* crypto::hash object.
*/
bool
get_tx_pub_key_from_str_hash(Blockchain& core_storage, const string& hash_str, transaction& tx)
{
crypto::hash tx_hash;
parse_hash256(hash_str, tx_hash);
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
try
{
// get transaction with given hash
tx = core_storage.get_db().get_tx(tx_hash);
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
catch (const TX_DNE& e)
2016-04-06 06:53:37 +00:00
{
2017-12-17 03:12:52 +00:00
cerr << e.what() << endl;
return false;
2017-04-28 07:24:19 +00:00
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
return true;
}
2016-04-06 06:53:37 +00:00
/**
2017-12-17 03:12:52 +00:00
* Parse monero address in a string form into
* cryptonote::account_public_address object
*/
bool
parse_str_address(const string& address_str,
address_parse_info& address_info,
bool testnet)
{
if (!get_account_address_from_str(address_info, testnet, address_str))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
cerr << "Error getting address: " << address_str << endl;
return false;
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
return true;
}
2017-12-17 03:12:52 +00:00
/**
* Return string representation of monero address
*/
string
print_address(const address_parse_info& address_info, bool testnet)
{
return "<" + get_account_address_as_str(
testnet, address_info.is_subaddress, address_info.address)
+ ">";
}
string
print_sig (const signature& sig)
{
stringstream ss;
ss << "c: <" << epee::string_tools::pod_to_hex(sig.c) << "> "
<< "r: <" << epee::string_tools::pod_to_hex(sig.r) << ">";
return ss.str();
}
/**
2017-12-17 03:12:52 +00:00
* Check if a character is a path seprator
*/
inline bool
is_separator(char c)
{
// default linux path separator
const char separator = PATH_SEPARARTOR;
2017-12-17 03:12:52 +00:00
return c == separator;
}
2016-04-06 06:53:37 +00:00
/**
2017-12-17 03:12:52 +00:00
* Remove trailinig path separator.
*/
string
remove_trailing_path_separator(const string& in_path)
{
string new_string = in_path;
if (!new_string.empty() && is_separator(new_string[new_string.size() - 1]))
new_string.erase(new_string.size() - 1);
return new_string;
}
2017-12-17 03:12:52 +00:00
bf::path
remove_trailing_path_separator(const bf::path& in_path)
{
2017-02-02 14:17:43 +00:00
#ifdef WIN32
2017-12-17 03:12:52 +00:00
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
string path_str = converter.to_bytes(in_path.native());
2017-02-02 14:17:43 +00:00
#else
2017-12-17 03:12:52 +00:00
string path_str = in_path.native();
2017-02-02 14:17:43 +00:00
#endif
2017-12-17 03:12:52 +00:00
return bf::path(remove_trailing_path_separator(path_str));
}
2016-04-06 06:53:37 +00:00
//string
//timestamp_to_str(time_t timestamp, const char* format)
//{
// return get_human_readable_timestamp(timestamp);
//}
2017-12-17 03:12:52 +00:00
string
timestamp_to_str_gm(time_t timestamp, const char* format)
{
const time_t* t = &timestamp;
2017-12-17 03:12:52 +00:00
const int TIME_LENGTH = 60;
2017-12-17 03:12:52 +00:00
char str_buff[TIME_LENGTH];
2017-12-17 03:12:52 +00:00
std::tm tmp;
gmtime_r(t, &tmp);
2017-12-17 03:12:52 +00:00
size_t len;
2017-12-17 03:12:52 +00:00
len = std::strftime(str_buff, TIME_LENGTH, format, &tmp);
2017-12-17 03:12:52 +00:00
return string(str_buff, len);
}
2017-12-17 03:12:52 +00:00
ostream&
operator<< (ostream& os, const address_parse_info& addr_info)
{
os << get_account_address_as_str(false, addr_info.is_subaddress, addr_info.address);
return os;
}
/*
2017-12-17 03:12:52 +00:00
* Generate key_image of foran ith output
*/
bool
generate_key_image(const crypto::key_derivation& derivation,
const std::size_t i,
const crypto::secret_key& sec_key,
const crypto::public_key& pub_key,
crypto::key_image& key_img)
{
2017-12-17 03:12:52 +00:00
cryptonote::keypair in_ephemeral;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
if (!crypto::derive_public_key(derivation, i,
pub_key,
in_ephemeral.pub))
{
cerr << "Error generating publick key " << pub_key << endl;
return false;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
try
{
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
crypto::derive_secret_key(derivation, i,
sec_key,
in_ephemeral.sec);
}
catch(const std::exception& e)
{
cerr << "Error generate secret image: " << e.what() << endl;
return false;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
try
{
crypto::generate_key_image(in_ephemeral.pub,
in_ephemeral.sec,
key_img);
}
catch(const std::exception& e)
{
cerr << "Error generate key image: " << e.what() << endl;
return false;
2017-04-28 07:24:19 +00:00
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
return true;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
string
get_default_lmdb_folder(bool testnet)
{
// default path to monero folder
// on linux this is /home/<username>/.bitmonero
string default_monero_dir = tools::get_default_data_dir();
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
if (testnet)
default_monero_dir += "/testnet";
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
// the default folder of the lmdb blockchain database
// is therefore as follows
return default_monero_dir + string("/lmdb");
}
2016-04-06 06:53:37 +00:00
/*
2017-12-17 03:12:52 +00:00
* Ge blockchain exception from command line option
*
* If not given, provide default path
*/
bool
get_blockchain_path(const boost::optional<string>& bc_path,
bf::path& blockchain_path,
bool testnet)
{
// the default folder of the lmdb blockchain database
string default_lmdb_dir = xmreg::get_default_lmdb_folder(testnet);
blockchain_path = bc_path
? bf::path(*bc_path)
: bf::path(default_lmdb_dir);
if (!bf::is_directory(blockchain_path))
2016-04-06 06:53:37 +00:00
{
2017-12-17 03:12:52 +00:00
cerr << "Given path \"" << blockchain_path << "\" "
<< "is not a folder or does not exist" << " "
<< endl;
return false;
}
2016-09-06 11:19:10 +00:00
2017-12-17 03:12:52 +00:00
blockchain_path = xmreg::remove_trailing_path_separator(blockchain_path);
2016-09-06 11:19:10 +00:00
2017-12-17 03:12:52 +00:00
return true;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t
sum_money_in_outputs(const transaction& tx)
{
uint64_t sum_xmr {0};
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
for (const tx_out& txout: tx.vout)
{
sum_xmr += txout.amount;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
pair<uint64_t, uint64_t>
sum_money_in_outputs(const string& json_str)
{
pair<uint64_t, uint64_t> sum_xmr {0, 0};
2017-12-17 03:12:52 +00:00
json j;
2017-12-17 03:12:52 +00:00
try
{
j = json::parse( json_str);
}
catch (std::invalid_argument& e)
{
cerr << "sum_money_in_outputs: " << e.what() << endl;
return sum_xmr;
}
2017-12-17 03:12:52 +00:00
for (json& vout: j["vout"])
{
2017-12-17 03:12:52 +00:00
sum_xmr.first += vout["amount"].get<uint64_t>();
++sum_xmr.second;
}
2016-12-02 01:03:33 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2017-12-17 03:12:52 +00:00
pair<uint64_t, uint64_t>
sum_money_in_outputs(const json& _json)
{
pair<uint64_t, uint64_t> sum_xmr {0ULL, 0ULL};
2017-12-17 03:12:52 +00:00
for (const json& vout: _json["vout"])
{
sum_xmr.first += vout["amount"].get<uint64_t>();
++sum_xmr.second;
}
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
array<uint64_t, 4>
summary_of_in_out_rct(
const transaction& tx,
vector<pair<txout_to_key, uint64_t>>& output_pub_keys,
vector<txin_to_key>& input_key_imgs)
{
2017-12-17 03:12:52 +00:00
uint64_t xmr_outputs {0};
uint64_t xmr_inputs {0};
uint64_t mixin_no {0};
uint64_t num_nonrct_inputs {0};
2017-12-17 03:12:52 +00:00
for (const tx_out& txout: tx.vout)
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
if (txout.target.type() != typeid(txout_to_key))
{
// push empty pair.
output_pub_keys.push_back(pair<txout_to_key, uint64_t>{});
continue;
}
2017-12-17 03:12:52 +00:00
// get tx input key
const txout_to_key& txout_key
= boost::get<cryptonote::txout_to_key>(txout.target);
2017-12-17 03:12:52 +00:00
output_pub_keys.push_back(make_pair(txout_key, txout.amount));
2017-12-17 03:12:52 +00:00
xmr_outputs += txout.amount;
}
2017-12-17 03:12:52 +00:00
size_t input_no = tx.vin.size();
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < input_no; ++i)
{
2017-12-17 03:12:52 +00:00
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
{
continue;
}
2017-12-17 03:12:52 +00:00
// get tx input key
const cryptonote::txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
xmr_inputs += tx_in_to_key.amount;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
if (tx_in_to_key.amount != 0)
{
++num_nonrct_inputs;
}
2017-12-17 03:12:52 +00:00
if (mixin_no == 0)
{
mixin_no = tx_in_to_key.key_offsets.size();
}
2017-12-17 03:12:52 +00:00
input_key_imgs.push_back(tx_in_to_key);
2017-12-17 03:12:52 +00:00
} // for (size_t i = 0; i < input_no; ++i)
2017-12-17 03:12:52 +00:00
return {xmr_outputs, xmr_inputs, mixin_no, num_nonrct_inputs};
};
2017-12-17 03:12:52 +00:00
// this version for mempool txs from json
array<uint64_t, 6>
summary_of_in_out_rct(const json& _json)
{
uint64_t xmr_outputs {0};
uint64_t xmr_inputs {0};
uint64_t no_outputs {0};
uint64_t no_inputs {0};
uint64_t mixin_no {0};
uint64_t num_nonrct_inputs {0};
for (const json& vout: _json["vout"])
{
xmr_outputs += vout["amount"].get<uint64_t>();
}
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
no_outputs = _json["vout"].size();
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
for (const json& vin: _json["vin"])
{
2017-12-17 03:12:52 +00:00
uint64_t amount = vin["key"]["amount"].get<uint64_t>();
2017-12-17 03:12:52 +00:00
xmr_inputs += amount;
2017-12-17 03:12:52 +00:00
if (amount != 0)
++num_nonrct_inputs;
}
2017-12-17 03:12:52 +00:00
no_inputs = _json["vin"].size();
2017-12-17 03:12:52 +00:00
mixin_no = _json["vin"].at(0)["key"]["key_offsets"].size() - 1;
2017-12-17 03:12:52 +00:00
return {xmr_outputs, xmr_inputs, no_outputs, no_inputs, mixin_no, num_nonrct_inputs};
};
2017-12-17 03:12:52 +00:00
uint64_t
sum_money_in_inputs(const transaction& tx)
{
uint64_t sum_xmr {0};
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
size_t input_no = tx.vin.size();
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < input_no; ++i)
2016-04-11 07:02:44 +00:00
{
2017-12-17 03:12:52 +00:00
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
continue;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const cryptonote::txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
sum_xmr += tx_in_to_key.amount;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
}
pair<uint64_t, uint64_t>
sum_money_in_inputs(const string& json_str)
{
pair<uint64_t, uint64_t> sum_xmr {0, 0};
2016-12-02 01:03:33 +00:00
2017-12-17 03:12:52 +00:00
json j;
try
{
j = json::parse( json_str);
}
catch (std::invalid_argument& e)
{
cerr << "sum_money_in_outputs: " << e.what() << endl;
return sum_xmr;
2016-12-02 01:03:33 +00:00
}
2017-12-17 03:12:52 +00:00
for (json& vin: j["vin"])
{
2017-12-17 03:12:52 +00:00
sum_xmr.first += vin["key"]["amount"].get<uint64_t>();
++sum_xmr.second;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2017-12-17 03:12:52 +00:00
pair<uint64_t, uint64_t>
sum_money_in_inputs(const json& _json)
{
pair<uint64_t, uint64_t> sum_xmr {0, 0};
2017-12-17 03:12:52 +00:00
for (const json& vin: _json["vin"])
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
sum_xmr.first += vin["key"]["amount"].get<uint64_t>();
++sum_xmr.second;
}
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
uint64_t
count_nonrct_inputs(const transaction& tx)
{
uint64_t num {0};
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
size_t input_no = tx.vin.size();
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < input_no; ++i)
{
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
continue;
}
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const cryptonote::txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
if (tx_in_to_key.amount != 0)
++num;
}
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
return num;
}
uint64_t
count_nonrct_inputs(const string& json_str)
{
uint64_t num {0};
2017-02-17 02:47:35 +00:00
2017-12-17 03:12:52 +00:00
json j;
try
{
j = json::parse( json_str);
}
catch (std::invalid_argument& e)
{
cerr << "count_nonrct_inputs: " << e.what() << endl;
2017-02-17 02:47:35 +00:00
return num;
}
2017-12-17 03:12:52 +00:00
for (json& vin: j["vin"])
2017-02-17 02:47:35 +00:00
{
2017-12-17 03:12:52 +00:00
uint64_t amount = vin["key"]["amount"].get<uint64_t>();
if (amount != 0)
++num;
}
2017-12-17 03:12:52 +00:00
return num;
};
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
uint64_t
count_nonrct_inputs(const json& _json)
{
uint64_t num {0};
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
for (const json& vin: _json["vin"])
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
uint64_t amount = vin["key"]["amount"].get<uint64_t>();
if (amount != 0)
++num;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
return num;
};
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
array<uint64_t, 2>
sum_money_in_tx(const transaction& tx)
{
array<uint64_t, 2> sum_xmr;
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
sum_xmr[0] = sum_money_in_inputs(tx);
sum_xmr[1] = sum_money_in_outputs(tx);
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
array<uint64_t, 2>
sum_money_in_txs(const vector<transaction>& txs)
{
array<uint64_t, 2> sum_xmr {0,0};
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
for (const transaction& tx: txs)
2016-04-12 03:31:26 +00:00
{
2017-12-17 03:12:52 +00:00
sum_xmr[0] += sum_money_in_inputs(tx);
sum_xmr[1] += sum_money_in_outputs(tx);
}
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
return sum_xmr;
};
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
uint64_t
sum_fees_in_txs(const vector<transaction>& txs)
{
uint64_t fees_sum {0};
2017-12-17 03:12:52 +00:00
for (const transaction& tx: txs)
{
2017-12-17 03:12:52 +00:00
fees_sum += get_tx_fee(tx);
}
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
return fees_sum;
}
2016-04-12 03:31:26 +00:00
2017-12-17 03:12:52 +00:00
vector<pair<txout_to_key, uint64_t>>
get_ouputs(const transaction& tx)
{
vector<pair<txout_to_key, uint64_t>> outputs;
2017-12-17 03:12:52 +00:00
for (const tx_out& txout: tx.vout)
2016-04-11 07:02:44 +00:00
{
2017-12-17 03:12:52 +00:00
if (txout.target.type() != typeid(txout_to_key))
2016-04-11 07:02:44 +00:00
{
2017-12-17 03:12:52 +00:00
// push empty pair.
outputs.push_back(pair<txout_to_key, uint64_t>{});
continue;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const txout_to_key& txout_key
= boost::get<cryptonote::txout_to_key>(txout.target);
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
outputs.push_back(make_pair(txout_key, txout.amount));
}
return outputs;
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
};
2016-05-11 06:32:02 +00:00
2017-12-17 03:12:52 +00:00
vector<tuple<txout_to_key, uint64_t, uint64_t>>
get_ouputs_tuple(const transaction& tx)
{
vector<tuple<txout_to_key, uint64_t, uint64_t>> outputs;
2017-12-17 03:12:52 +00:00
for (uint64_t n = 0; n < tx.vout.size(); ++n)
2016-05-11 06:32:02 +00:00
{
2017-12-17 03:12:52 +00:00
if (tx.vout[n].target.type() != typeid(txout_to_key))
2016-05-11 06:32:02 +00:00
{
2017-12-17 03:12:52 +00:00
continue;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const txout_to_key& txout_key
= boost::get<cryptonote::txout_to_key>(tx.vout[n].target);
2016-05-11 06:32:02 +00:00
2017-12-17 03:12:52 +00:00
outputs.push_back(make_tuple(txout_key, tx.vout[n].amount, n));
}
2016-05-11 06:32:02 +00:00
2017-12-17 03:12:52 +00:00
return outputs;
};
uint64_t
get_mixin_no(const transaction& tx)
{
uint64_t mixin_no {0};
2016-05-11 06:32:02 +00:00
2017-12-17 03:12:52 +00:00
size_t input_no = tx.vin.size();
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < input_no; ++i)
{
2017-12-17 03:12:52 +00:00
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
continue;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
2017-12-17 03:12:52 +00:00
mixin_no = tx_in_to_key.key_offsets.size();
2017-12-17 03:12:52 +00:00
// look for first mixin number.
// all inputs in a single transaction have same number
if (mixin_no > 0)
{
break;
2017-04-28 07:24:19 +00:00
}
}
2017-12-17 03:12:52 +00:00
return mixin_no;
}
vector<uint64_t>
get_mixin_no(const string& json_str)
{
vector<uint64_t> mixin_no;
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
json j;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
try
{
j = json::parse(json_str);
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
mixin_no.push_back(j["vin"].at(0)["key"]["key_offsets"].size());
}
2017-12-17 03:12:52 +00:00
catch (std::invalid_argument& e)
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
cerr << "get_mixin_no: " << e.what() << endl;
2017-04-28 07:24:19 +00:00
return mixin_no;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
return mixin_no;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
vector<uint64_t>
get_mixin_no(const json& _json)
{
vector<uint64_t> mixin_no;
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
mixin_no.push_back(_json["vin"].at(0)["key"]["key_offsets"].size());
return mixin_no;
}
2016-04-12 03:31:26 +00:00
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
vector<uint64_t>
get_mixin_no_in_txs(const vector<transaction>& txs)
{
vector<uint64_t> mixin_no;
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
for (const transaction& tx: txs)
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
mixin_no.push_back(get_mixin_no(tx));
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
return mixin_no;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
vector<txin_to_key>
get_key_images(const transaction& tx)
{
vector<txin_to_key> key_images;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
size_t input_no = tx.vin.size();
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < input_no; ++i)
{
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
if(tx.vin[i].type() != typeid(txin_to_key))
{
continue;
}
2016-04-11 07:02:44 +00:00
2017-12-17 03:12:52 +00:00
// get tx input key
const txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
2017-12-17 03:12:52 +00:00
key_images.push_back(tx_in_to_key);
}
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
return key_images;
}
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
bool
get_payment_id(const vector<uint8_t>& extra,
crypto::hash& payment_id,
crypto::hash8& payment_id8)
{
payment_id = null_hash;
payment_id8 = null_hash8;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
std::vector<tx_extra_field> tx_extra_fields;
2017-12-17 03:12:52 +00:00
if(!parse_tx_extra(extra, tx_extra_fields))
{
2017-04-28 07:24:19 +00:00
return false;
}
2017-12-17 03:12:52 +00:00
tx_extra_nonce extra_nonce;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
// first check for encrypted id and then for normal one
if(get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8))
{
return true;
}
else if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
{
return true;
}
2017-04-28 07:24:19 +00:00
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
return false;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
bool
get_payment_id(const transaction& tx,
crypto::hash& payment_id,
crypto::hash8& payment_id8)
{
return get_payment_id(tx.extra, payment_id, payment_id8);
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
array<size_t, 5>
timestamp_difference(uint64_t t1, uint64_t t2)
{
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t timestamp_diff = t1 - t2;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
// calculate difference of timestamps from current block to the mixin one
if (t2 > t1)
{
timestamp_diff = t2 - t1;
}
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t time_diff_years = timestamp_diff / 31536000;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
timestamp_diff -= time_diff_years * 31536000;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t time_diff_days = timestamp_diff / 86400;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
timestamp_diff -= time_diff_days * 86400;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t time_diff_hours = timestamp_diff / 3600;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
timestamp_diff -= time_diff_hours * 3600;
2016-04-06 06:53:37 +00:00
2017-12-17 03:12:52 +00:00
uint64_t time_diff_minutes = timestamp_diff / 60;
2016-04-08 06:14:42 +00:00
2017-12-17 03:12:52 +00:00
timestamp_diff -= time_diff_minutes * 60;
2016-04-08 06:14:42 +00:00
2017-12-17 03:12:52 +00:00
uint64_t time_diff_seconds = timestamp_diff ;
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
return array<size_t, 5> {time_diff_years, time_diff_days,
time_diff_hours, time_diff_minutes,
time_diff_seconds};
};
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
string
read(string filename)
{
if (!bf::exists(bf::path(filename)))
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
cerr << "File does not exist: " << filename << endl;
return string();
}
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
std::ifstream t(filename);
return string(std::istreambuf_iterator<char>(t),
std::istreambuf_iterator<char>());
}
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
pair<string, double>
timestamps_time_scale(const vector<uint64_t>& timestamps,
uint64_t timeN,
uint64_t resolution,
uint64_t time0)
{
string empty_time = string(resolution, '_');
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
size_t time_axis_length = empty_time.size();
2016-09-07 00:01:07 +00:00
2017-12-17 03:12:52 +00:00
uint64_t interval_length = timeN-time0;
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
double scale = double(interval_length) / double(time_axis_length);
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
for (const auto& timestamp: timestamps)
{
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
if (timestamp < time0 || timestamp > timeN)
{
cout << "Out of range" << endl;
continue;
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
uint64_t timestamp_place = double(timestamp-time0)
/ double(interval_length)*(time_axis_length - 1);
empty_time[timestamp_place + 1] = '*';
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
return make_pair(empty_time, scale);
}
2017-12-17 03:12:52 +00:00
bool
decode_ringct(const rct::rctSig& rv,
const crypto::public_key pub,
const crypto::secret_key &sec,
unsigned int i,
rct::key & mask,
uint64_t & amount)
{
crypto::key_derivation derivation;
2017-12-17 03:12:52 +00:00
bool r = crypto::generate_key_derivation(pub, sec, derivation);
2017-12-17 03:12:52 +00:00
if (!r)
{
cerr <<"Failed to generate key derivation to decode rct output " << i << endl;
return false;
}
2017-12-17 03:12:52 +00:00
return decode_ringct(rv, derivation, i, mask, amount);
}
bool
decode_ringct(rct::rctSig const& rv,
crypto::key_derivation const& derivation,
unsigned int i,
rct::key& mask,
uint64_t& amount)
{
try
{
2017-12-17 03:12:52 +00:00
crypto::secret_key scalar1;
2017-12-17 03:12:52 +00:00
crypto::derivation_to_scalar(derivation, i, scalar1);
2017-12-17 03:12:52 +00:00
switch (rv.type)
{
2017-12-17 03:12:52 +00:00
case rct::RCTTypeSimple:
case rct::RCTTypeSimpleBulletproof:
amount = rct::decodeRctSimple(rv,
rct::sk2rct(scalar1),
i,
mask,
hw::get_device("default"));
2017-12-17 03:12:52 +00:00
break;
case rct::RCTTypeFull:
case rct::RCTTypeFullBulletproof:
amount = rct::decodeRct(rv,
rct::sk2rct(scalar1),
i,
mask,
hw::get_device("default"));
2017-12-17 03:12:52 +00:00
break;
default:
cerr << "Unsupported rct type: " << rv.type << '\n';
return false;
}
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
catch (...)
{
cerr << "Failed to decode input " << i << '\n';
return false;
}
return true;
}
2016-04-20 07:11:43 +00:00
2017-12-17 03:12:52 +00:00
bool
url_decode(const std::string& in, std::string& out)
{
out.clear();
out.reserve(in.size());
for (std::size_t i = 0; i < in.size(); ++i)
2016-09-27 23:44:39 +00:00
{
2017-12-17 03:12:52 +00:00
if (in[i] == '%')
2016-09-27 23:44:39 +00:00
{
2017-12-17 03:12:52 +00:00
if (i + 3 <= in.size())
2016-09-27 23:44:39 +00:00
{
2017-12-17 03:12:52 +00:00
int value = 0;
std::istringstream is(in.substr(i + 1, 2));
if (is >> std::hex >> value)
2016-09-27 23:44:39 +00:00
{
2017-12-17 03:12:52 +00:00
out += static_cast<char>(value);
i += 2;
2016-09-27 23:44:39 +00:00
}
else
{
return false;
}
}
else
{
2017-12-17 03:12:52 +00:00
return false;
2016-09-27 23:44:39 +00:00
}
}
2017-12-17 03:12:52 +00:00
else if (in[i] == '+')
{
out += ' ';
}
else
{
out += in[i];
}
2016-09-27 23:44:39 +00:00
}
2017-12-17 03:12:52 +00:00
return true;
}
2016-09-27 23:44:39 +00:00
2017-12-17 03:12:52 +00:00
map<std::string, std::string>
parse_crow_post_data(const string& req_body)
{
map<std::string, std::string> body;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
vector<string> vec;
string tmp;
bool result = url_decode(req_body, tmp);
if (result)
{
boost::algorithm::split(vec, tmp,
[](char x) {return x == '&';
});
2017-12-17 03:12:52 +00:00
for(auto &it : vec)
2016-09-27 23:44:39 +00:00
{
2017-12-17 03:12:52 +00:00
auto pos = it.find("=");
2017-12-17 03:12:52 +00:00
if (pos != string::npos)
body[it.substr(0, pos)] = it.substr(pos + 1);
else
break;
2016-09-27 23:44:39 +00:00
}
}
2017-12-17 03:12:52 +00:00
return body;
}
// from wallet2::decrypt
2017-12-17 03:12:52 +00:00
string
decrypt(const std::string &ciphertext,
const crypto::secret_key &skey,
bool authenticated)
{
2017-12-28 00:18:58 +00:00
const size_t prefix_size = sizeof(chacha_iv)
2017-12-17 03:12:52 +00:00
+ (authenticated ? sizeof(crypto::signature) : 0);
if (ciphertext.size() < prefix_size)
{
cerr << "Unexpected ciphertext size" << endl;
return {};
}
2017-12-28 00:18:58 +00:00
crypto::chacha_key key;
crypto::generate_chacha_key(&skey, sizeof(skey), key);
2017-12-28 00:18:58 +00:00
const crypto::chacha_iv &iv = *(const crypto::chacha_iv*)&ciphertext[0];
2017-12-17 03:12:52 +00:00
std::string plaintext;
2017-12-17 03:12:52 +00:00
plaintext.resize(ciphertext.size() - prefix_size);
2017-12-17 03:12:52 +00:00
if (authenticated)
{
crypto::hash hash;
crypto::cn_fast_hash(ciphertext.data(), ciphertext.size() - sizeof(signature), hash);
crypto::public_key pkey;
crypto::secret_key_to_public_key(skey, pkey);
2017-12-17 03:12:52 +00:00
const crypto::signature &signature =
*(const crypto::signature*)&ciphertext[ciphertext.size()
- sizeof(crypto::signature)];
2016-11-18 01:00:35 +00:00
2017-12-17 03:12:52 +00:00
if (!crypto::check_signature(hash, pkey, signature))
{
cerr << "Failed to authenticate criphertext" << endl;
return {};
2017-04-28 07:24:19 +00:00
}
}
2017-12-17 03:12:52 +00:00
crypto::chacha8(ciphertext.data() + sizeof(iv),
ciphertext.size() - prefix_size,
key, iv, &plaintext[0]);
return plaintext;
}
// based on
// crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const
2017-12-17 03:12:52 +00:00
public_key
get_tx_pub_key_from_received_outs(const transaction &tx)
{
std::vector<tx_extra_field> tx_extra_fields;
2017-12-17 03:12:52 +00:00
if(!parse_tx_extra(tx.extra, tx_extra_fields))
{
// Extra may only be partially parsed, it's OK if tx_extra_fields contains public key
}
2017-12-17 03:12:52 +00:00
// Due to a previous bug, there might be more than one tx pubkey in extra, one being
// the result of a previously discarded signature.
// For speed, since scanning for outputs is a slow process, we check whether extra
// contains more than one pubkey. If not, the first one is returned. If yes, they're
// checked for whether they yield at least one output
tx_extra_pub_key pub_key_field;
2017-12-17 03:12:52 +00:00
if (!find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, 0))
{
return null_pkey;
}
2017-12-17 03:12:52 +00:00
public_key tx_pub_key = pub_key_field.pub_key;
2017-12-17 03:12:52 +00:00
bool two_found = find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, 1);
2017-12-17 03:12:52 +00:00
if (!two_found)
{
// easy case, just one found
return tx_pub_key;
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
else
{
// just return second one if there are two.
// this does not require private view key, as
// its not needed for my use case.
return pub_key_field.pub_key;
}
return null_pkey;
}
/**
* Check if given output (specified by output_index)
* belongs is ours based
* on our private view key and public spend key
*/
2017-12-17 03:12:52 +00:00
bool
is_output_ours(const size_t& output_index,
const transaction& tx,
const public_key& pub_tx_key,
const secret_key& private_view_key,
const public_key& public_spend_key)
{
// public transaction key is combined with our viewkey
// to create, so called, derived key.
key_derivation derivation;
2016-12-02 02:22:49 +00:00
2017-12-17 03:12:52 +00:00
if (!generate_key_derivation(pub_tx_key, private_view_key, derivation))
{
cerr << "Cant get dervied key for: " << "\n"
<< "pub_tx_key: " << pub_tx_key << " and "
<< "prv_view_key" << private_view_key << endl;
2016-12-02 02:22:49 +00:00
return false;
}
2017-12-17 03:12:52 +00:00
// get the tx output public key
// that normally would be generated for us,
// if someone had sent us some xmr.
public_key pubkey;
2017-12-17 03:12:52 +00:00
derive_public_key(derivation,
output_index,
public_spend_key,
pubkey);
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
//cout << "\n" << tx.vout.size() << " " << output_index << endl;
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
// get tx output public key
const txout_to_key tx_out_to_key
= boost::get<txout_to_key>(tx.vout[output_index].target);
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
if (tx_out_to_key.key == pubkey)
2016-12-02 06:38:51 +00:00
{
2017-12-17 03:12:52 +00:00
return true;
}
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
return false;
}
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
bool
get_real_output_for_key_image(const key_image& ki,
const transaction& tx,
const secret_key& private_view_key,
const public_key& public_spend_key,
uint64_t output_idx,
public_key output_pub_key)
{
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
return false;
}
2016-12-02 06:38:51 +00:00
2017-12-17 03:12:52 +00:00
string
make_printable(const string& in_s)
{
string output;
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
for (char c: in_s)
{
2017-12-17 03:12:52 +00:00
if (isprint(c))
{
2017-12-17 03:12:52 +00:00
output += c;
}
2017-12-17 03:12:52 +00:00
else
{
2017-12-17 03:12:52 +00:00
switch(c){
case '\000': output += "\\000";break;
case '\001': output += "\\001";break;
case '\002': output += "\\002";break;
case '\003': output += "\\003";break;
case '\004': output += "\\004";break;
case '\005': output += "\\005";break;
case '\006': output += "\\006";break;
case '\007': output += "\\007";break;
// there are more case but for now its ok
default:
stringstream ss;
ss << std::hex << (int)c;
output += "0x" + ss.str();
break;
2016-12-02 06:38:51 +00:00
}
}
2017-04-28 07:24:19 +00:00
}
2017-12-17 03:12:52 +00:00
return output;
}
2017-04-28 07:24:19 +00:00
2017-12-17 03:12:52 +00:00
string
get_human_readable_timestamp(uint64_t ts)
{
char buffer[64];
if (ts < 1234567890)
return "<unknown>";
2017-12-17 03:12:52 +00:00
time_t tt = ts;
2017-12-17 03:12:52 +00:00
struct tm tm;
2017-12-17 03:12:52 +00:00
gmtime_r(&tt, &tm);
2017-12-17 03:12:52 +00:00
strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M:%S", &tm);
2017-12-17 03:12:52 +00:00
return std::string(buffer);
}
2017-12-17 03:12:52 +00:00
void
pause_execution(uint64_t no_seconds, const string& text)
{
2017-12-17 03:12:52 +00:00
cout << "\nPausing " << text
<< " for " << no_seconds << " seconds: "
<< flush;
2017-12-17 03:12:52 +00:00
for (size_t i = 0; i < no_seconds; ++i)
2017-04-28 07:24:19 +00:00
{
2017-12-17 03:12:52 +00:00
cout << "." << flush;
std::this_thread::sleep_for(std::chrono::seconds(1));
2017-03-03 04:56:42 +00:00
}
2017-12-17 03:12:52 +00:00
cout << endl;
}
2017-03-03 04:56:42 +00:00
}