2016-04-06 06:53:37 +00:00
|
|
|
//
|
2016-11-23 22:11:23 +00:00
|
|
|
// Created by mwo on 5/11/15.
|
2016-04-06 06:53:37 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef XMREG01_TOOLS_H
|
|
|
|
#define XMREG01_TOOLS_H
|
|
|
|
|
|
|
|
#define PATH_SEPARARTOR '/'
|
|
|
|
|
2016-04-12 03:31:26 +00:00
|
|
|
#define XMR_AMOUNT(value) \
|
|
|
|
static_cast<double>(value) / 1e12
|
|
|
|
|
2016-04-13 08:29:01 +00:00
|
|
|
#define REMOVE_HASH_BRAKETS(a_hash) \
|
2016-04-13 21:05:42 +00:00
|
|
|
a_hash.substr(1, a_hash.size()-2)
|
2016-04-13 08:29:01 +00:00
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
#include "monero_headers.h"
|
|
|
|
|
2016-04-12 03:31:26 +00:00
|
|
|
#include "../ext/infix_iterator.h"
|
2016-11-22 09:34:38 +00:00
|
|
|
#include "../ext/date/tz.h"
|
2016-11-24 03:28:54 +00:00
|
|
|
#include "../ext/format.h"
|
2016-12-01 07:13:29 +00:00
|
|
|
#include "../ext/json.hpp"
|
2016-12-02 01:03:33 +00:00
|
|
|
#include "../ext/member_checker.h"
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-04-13 08:46:08 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2016-04-06 06:53:37 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
2016-04-13 08:46:08 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some helper functions used in the example.
|
|
|
|
* Names are rather self-explanatory, so I think
|
|
|
|
* there is no reason for any detailed explanations here
|
|
|
|
*/
|
|
|
|
namespace xmreg
|
|
|
|
{
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
using namespace cryptonote;
|
|
|
|
using namespace crypto;
|
|
|
|
using namespace std;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
namespace pt = boost::posix_time;
|
|
|
|
namespace gt = boost::gregorian;
|
|
|
|
namespace lt = boost::local_time;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-12-02 00:09:09 +00:00
|
|
|
using json = nlohmann::json;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
struct outputs_visitor
|
|
|
|
{
|
|
|
|
std::vector<crypto::public_key >& m_output_keys;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
const Blockchain& m_bch;
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
outputs_visitor(std::vector<crypto::public_key>& output_keys, const Blockchain& bch) :
|
|
|
|
m_output_keys(output_keys), m_bch(bch)
|
|
|
|
{
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool handle_output(uint64_t unlock_time, const crypto::public_key &pubkey)
|
|
|
|
{
|
|
|
|
m_output_keys.push_back(pubkey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +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
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
get_tx_pub_key_from_str_hash(Blockchain& core_storage,
|
|
|
|
const string& hash_str,
|
|
|
|
transaction& tx);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
parse_str_address(const string& address_str,
|
|
|
|
account_public_address& address,
|
|
|
|
bool testnet = false);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
inline bool
|
|
|
|
is_separator(char c);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
print_address(const account_public_address& address,
|
|
|
|
bool testnet = false);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
print_sig (const signature& sig);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
remove_trailing_path_separator(const string& in_path);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bf::path
|
|
|
|
remove_trailing_path_separator(const bf::path& in_path);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
timestamp_to_str(time_t timestamp, const char* format = "%F %T");
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-24 05:54:15 +00:00
|
|
|
string
|
|
|
|
timestamp_to_str_local(time_t timestamp, const char* format = "%F %T");
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
ostream&
|
|
|
|
operator<< (ostream& os, const account_public_address& addr);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
get_default_lmdb_folder(bool testnet = false);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
generate_key_image(const crypto::key_derivation& derivation,
|
|
|
|
const std::size_t output_index,
|
|
|
|
const crypto::secret_key& sec_key,
|
|
|
|
const crypto::public_key& pub_key,
|
|
|
|
crypto::key_image& key_img);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
get_blockchain_path(const boost::optional<string>& bc_path,
|
|
|
|
bf::path& blockchain_path,
|
|
|
|
bool testnet = false);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
uint64_t
|
|
|
|
sum_money_in_outputs(const transaction& tx);
|
2016-04-11 07:02:44 +00:00
|
|
|
|
2016-12-02 00:09:09 +00:00
|
|
|
pair<uint64_t, uint64_t>
|
|
|
|
sum_money_in_outputs(const string& json_str);
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
uint64_t
|
|
|
|
sum_money_in_inputs(const transaction& tx);
|
2016-04-11 07:02:44 +00:00
|
|
|
|
2016-12-02 01:03:33 +00:00
|
|
|
pair<uint64_t, uint64_t>
|
|
|
|
sum_money_in_inputs(const string& json_str);
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
array<uint64_t, 2>
|
|
|
|
sum_money_in_tx(const transaction& tx);
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
array<uint64_t, 2>
|
|
|
|
sum_money_in_txs(const vector<transaction>& txs);
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
uint64_t
|
|
|
|
sum_fees_in_txs(const vector<transaction>& txs);
|
2016-04-12 04:39:52 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
uint64_t
|
|
|
|
get_mixin_no(const transaction& tx);
|
2016-04-11 07:02:44 +00:00
|
|
|
|
2016-12-02 01:03:33 +00:00
|
|
|
vector<uint64_t>
|
|
|
|
get_mixin_no(const string& json_str);
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
vector<uint64_t>
|
|
|
|
get_mixin_no_in_txs(const vector<transaction>& txs);
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
vector<pair<txout_to_key, uint64_t>>
|
|
|
|
get_ouputs(const transaction& tx);
|
2016-04-11 07:02:44 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
vector<tuple<txout_to_key, uint64_t, uint64_t>>
|
|
|
|
get_ouputs_tuple(const transaction& tx);
|
2016-05-11 06:32:02 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
vector<txin_to_key>
|
|
|
|
get_key_images(const transaction& tx);
|
2016-04-11 07:02:44 +00:00
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
get_payment_id(const vector<uint8_t>& extra,
|
|
|
|
crypto::hash& payment_id,
|
|
|
|
crypto::hash8& payment_id8);
|
2016-10-19 02:46:33 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
get_payment_id(const transaction& tx,
|
|
|
|
crypto::hash& payment_id,
|
|
|
|
crypto::hash8& payment_id8);
|
2016-04-25 03:18:56 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
inline void
|
|
|
|
enable_monero_log() {
|
|
|
|
uint32_t log_level = 0;
|
|
|
|
epee::log_space::get_set_log_detalisation_level(true, log_level);
|
|
|
|
epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL);
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
inline double
|
|
|
|
get_xmr(uint64_t core_amount)
|
|
|
|
{
|
|
|
|
return static_cast<double>(core_amount) / 1e12;
|
|
|
|
}
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
array<size_t, 5>
|
|
|
|
timestamp_difference(uint64_t t1, uint64_t t2);
|
2016-04-06 06:53:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
string
|
|
|
|
read(string filename);
|
2016-04-08 06:14:42 +00:00
|
|
|
|
2016-04-12 03:31:26 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
/**
|
|
|
|
* prints an iterable such as vector
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void print_iterable(const T & elems) {
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
infix_ostream_iterator<typename T::value_type>
|
|
|
|
oiter(std::cout, ",");
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
std::cout << "[";
|
|
|
|
std::copy(elems.begin(), elems.end(),oiter);
|
|
|
|
std::cout << "]" << std::endl;
|
|
|
|
}
|
2016-04-12 03:31:26 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
pair<string, double>
|
|
|
|
timestamps_time_scale(const vector<uint64_t>& timestamps,
|
|
|
|
uint64_t timeN, uint64_t resolution = 80,
|
|
|
|
uint64_t time0 = 1397818193 /* timestamp of the second block */);
|
2016-04-20 07:11:43 +00:00
|
|
|
|
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
time_t
|
|
|
|
ptime_to_time_t(const pt::ptime& in_ptime);
|
2016-11-22 05:50:35 +00:00
|
|
|
|
2016-11-23 22:11:23 +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);
|
2016-09-07 06:50:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
bool
|
|
|
|
url_decode(const std::string& in, std::string& out);
|
2016-09-07 06:50:37 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
map<std::string, std::string>
|
|
|
|
parse_crow_post_data(const string& req_body);
|
2016-09-28 06:37:53 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
// from wallet2::decrypt
|
|
|
|
string
|
|
|
|
decrypt(const std::string &ciphertext,
|
|
|
|
const crypto::secret_key &skey,
|
|
|
|
bool authenticated = true);
|
2016-11-17 08:01:41 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
// based on
|
|
|
|
// crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const
|
|
|
|
public_key
|
|
|
|
get_tx_pub_key_from_received_outs(const transaction &tx);
|
2016-11-22 09:03:19 +00:00
|
|
|
|
2016-11-23 22:11:23 +00:00
|
|
|
date::sys_seconds
|
|
|
|
parse(const std::string& str, string format="%Y-%m-%d %H:%M:%S");
|
2016-11-23 05:32:51 +00:00
|
|
|
|
2016-11-24 03:28:54 +00:00
|
|
|
static
|
|
|
|
string
|
|
|
|
xmr_amount_to_str(const uint64_t& xmr_amount, string format="{:0.12f}")
|
|
|
|
{
|
|
|
|
return fmt::format("{:0.12f}", XMR_AMOUNT(xmr_amount));
|
|
|
|
}
|
|
|
|
|
2016-11-28 03:35:23 +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);
|
|
|
|
|
2016-11-29 01:21:32 +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-03 04:05:31 +00:00
|
|
|
// based on http://stackoverflow.com/a/9943098/248823
|
|
|
|
template<typename Iterator, typename Func>
|
|
|
|
void chunks(Iterator begin,
|
|
|
|
Iterator end,
|
|
|
|
iterator_traits<string::iterator>::difference_type k,
|
|
|
|
Func f)
|
|
|
|
{
|
|
|
|
Iterator chunk_begin;
|
|
|
|
Iterator chunk_end;
|
|
|
|
chunk_end = chunk_begin = begin;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(std::distance(chunk_end, end) < k)
|
|
|
|
chunk_end = end;
|
|
|
|
else
|
|
|
|
std::advance(chunk_end, k);
|
|
|
|
f(chunk_begin,chunk_end);
|
|
|
|
chunk_begin = chunk_end;
|
|
|
|
}
|
|
|
|
while(std::distance(chunk_begin,end) > 0);
|
|
|
|
}
|
|
|
|
|
2016-12-02 02:22:49 +00:00
|
|
|
bool
|
|
|
|
make_tx_from_json(const string& json_str, transaction& tx);
|
|
|
|
|
2016-04-06 06:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //XMREG01_TOOLS_H
|