key img nad monero tfer utils (w/o construct tx port yet) compiling w monero src update

pull/29/head
Paul Shapiro 6 years ago
parent 5f54c5aa8f
commit 22bb35bf01

@ -53,6 +53,9 @@ include_directories("${MONERO_SRC}/common")
include_directories("${MONERO_SRC}/vtlogger")
include_directories("${MONERO_SRC}/crypto")
include_directories("${MONERO_SRC}/cryptonote_basic")
include_directories("${MONERO_SRC}/multisig")
include_directories("${MONERO_SRC}/cryptonote_core")
include_directories("${MONERO_SRC}/mnemonics")
include_directories("test")
# keeping test files in a separate source directory
@ -65,24 +68,56 @@ set(
src/monero_address_utils.cpp
src/monero_paymentID_utils.hpp
src/monero_paymentID_utils.cpp
# src/monero_wallet_utils.hpp
# src/monero_wallet_utils.cpp
src/monero_key_image_utils.hpp
src/monero_key_image_utils.cpp
src/monero_transfer_utils.hpp
src/monero_transfer_utils.cpp
src/monero_wallet_utils.hpp
src/monero_wallet_utils.cpp
src/tools__ret_vals.hpp
src/tools__ret_vals.cpp
#
${MONERO_SRC}/cryptonote_basic/cryptonote_basic_impl.cpp
${MONERO_SRC}/cryptonote_basic/account.cpp
${MONERO_SRC}/cryptonote_basic/cryptonote_format_utils.cpp
${MONERO_SRC}/crypto/crypto.cpp
${MONERO_SRC}/crypto/hash.c
${MONERO_SRC}/crypto/slow-hash.c
${MONERO_SRC}/crypto/oaes_lib.c
${MONERO_SRC}/crypto/crypto-ops.c
${MONERO_SRC}/crypto/crypto-ops-data.c
${MONERO_SRC}/crypto/keccak.c
${MONERO_SRC}/crypto/random.c
${MONERO_SRC}/crypto/aesb.c
${MONERO_SRC}/crypto/tree-hash.c
${MONERO_SRC}/crypto/hash-extra-blake.c
${MONERO_SRC}/crypto/blake256.c
${MONERO_SRC}/crypto/hash-extra-groestl.c
${MONERO_SRC}/crypto/hash-extra-jh.c
${MONERO_SRC}/crypto/hash-extra-skein.c
${MONERO_SRC}/crypto/groestl.c
${MONERO_SRC}/crypto/jh.c
${MONERO_SRC}/crypto/skein.c
${MONERO_SRC}/cryptonote_core/cryptonote_tx_utils.cpp
${MONERO_SRC}/common/base58.cpp
${MONERO_SRC}/common/threadpool.cpp
${MONERO_SRC}/epee/src/hex.cpp
${MONERO_SRC}/epee/src/string_tools.cpp
${MONERO_SRC}/epee/src/memwipe.c
${MONERO_SRC}/epee/src/wipeable_string.cpp
${MONERO_SRC}/device/device.cpp
${MONERO_SRC}/device/device_default.cpp
${MONERO_SRC}/ringct/rctOps.cpp
${MONERO_SRC}/ringct/rctTypes.cpp
${MONERO_SRC}/ringct/rctCryptoOps.c
${MONERO_SRC}/ringct/rctSigs.cpp
${MONERO_SRC}/ringct/bulletproofs.cc
${MONERO_SRC}/mnemonics/electrum-words.cpp
${MONERO_SRC}/vtlogger/logger.cpp
)
# needed for slow-hash
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -maes")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes")
#Run through each source
foreach(testSrc ${TEST_SRCS})

@ -1 +1 @@
Subproject commit 40db201ddff523365b1625135a69e2ed80cce486
Subproject commit 34fa840ba48dcd1cdd4e653050e950b58dfbf524

@ -53,7 +53,8 @@ DecodedAddress_RetVals address_utils::decodedAddress(const string &addressString
addressString
);
if (didSucceed == false) {
retVals.err_str = "Invalid address"; // TODO: return code
retVals.did_error = true;
retVals.err_string = "Invalid address"; // TODO: return code
//
return retVals;
}

@ -0,0 +1,131 @@
//
// monero_key_image_utils.cpp
// MyMonero
//
// Created by Paul Shapiro on 1/2/18.
// Copyright (c) 2014-2018, MyMonero.com
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 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.
//
// 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.
//
// 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.
//
//
#include "monero_key_image_utils.hpp"
//
using namespace crypto;
using namespace cryptonote;
//
bool monero_key_image_utils::new__key_image(
const crypto::public_key& account_pub_spend_key,
const crypto::secret_key& account_sec_spend_key,
const crypto::secret_key& account_sec_view_key,
const crypto::public_key& tx_public_key,
uint64_t out_index,
KeyImageRetVals &retVals
) {
retVals = {};
//
bool r = false;
//
// "Subaddresses aren't supported in mymonero/openmonero yet. Roll out the original scheme:
// compute D = a*R
// compute P = Hs(D || i)*G + B
// compute x = Hs(D || i) + b (and check if P==x*G)
// compute I = x*Hp(P)"
crypto::key_derivation derivation;
r = crypto::generate_key_derivation(tx_public_key, account_sec_view_key, derivation);
if (!r) {
retVals.did_error = true;
std::stringstream ss{};
ss << "failed to generate_key_derivation(" << tx_public_key << ", " << account_sec_view_key << ")";
retVals.err_string = ss.str();
//
return false;
}
cryptonote::keypair in_ephemeral;
r = crypto::derive_public_key(derivation, out_index, account_pub_spend_key, in_ephemeral.pub);
if (!r) {
retVals.did_error = true;
std::stringstream ss{};
ss << "failed to derive_public_key (" << derivation << ", " << out_index << ", " << account_pub_spend_key << ")";
retVals.err_string = ss.str();
//
return false;
}
crypto::derive_secret_key(derivation, out_index, account_sec_spend_key, in_ephemeral.sec);
crypto::public_key out_pkey_test;
r = crypto::secret_key_to_public_key(in_ephemeral.sec, out_pkey_test);
if (!r) {
retVals.did_error = true;
std::stringstream ss{};
ss << "failed to secret_key_to_public_key(" << in_ephemeral.sec << ")";
retVals.err_string = ss.str();
//
return false;
}
if (in_ephemeral.pub != out_pkey_test) {
retVals.did_error = true;
retVals.err_string = "derived secret key doesn't match derived public key";
//
return false;
}
crypto::generate_key_image(in_ephemeral.pub, in_ephemeral.sec, retVals.calculated_key_image);
//
return true;
}
//+ (NSString *)new_keyImageFrom_tx_pub_key:(NSString *)tx_pub_key_NSString
//sec_spendKey:(NSString *)sec_spendKey_NSString
//sec_viewKey:(NSString *)sec_viewKey_NSString
//pub_spendKey:(NSString *)pub_spendKey_NSString
//out_index:(uint64_t)out_index
//{
// crypto::secret_key sec_viewKey{};
// crypto::secret_key sec_spendKey{};
// crypto::public_key pub_spendKey{};
// crypto::public_key tx_pub_key{};
// { // Would be nice to find a way to avoid converting these back and forth
// bool r = false;
// r = string_tools::hex_to_pod(std::string(sec_viewKey_NSString.UTF8String), sec_viewKey);
// NSAssert(r, @"Invalid secret view key");
// r = string_tools::hex_to_pod(std::string(sec_spendKey_NSString.UTF8String), sec_spendKey);
// NSAssert(r, @"Invalid secret spend key");
// r = string_tools::hex_to_pod(std::string(pub_spendKey_NSString.UTF8String), pub_spendKey);
// NSAssert(r, @"Invalid public spend key");
// r = string_tools::hex_to_pod(std::string(tx_pub_key_NSString.UTF8String), tx_pub_key);
// NSAssert(r, @"Invalid tx pub key");
// }
// monero_key_image_utils::KeyImageRetVals retVals;
// {
// bool r = monero_key_image_utils::new__key_image(pub_spendKey, sec_spendKey, sec_viewKey, tx_pub_key, out_index, retVals);
// if (!r) {
// return nil; // TODO: return error string? (unwrap optional)
// }
// }
// std::string key_image_hex_string = string_tools::pod_to_hex(retVals.calculated_key_image);
// NSString *key_image_hex_NSString = [NSString stringWithUTF8String:key_image_hex_string.c_str()];
// //
// return key_image_hex_NSString;
//}

@ -0,0 +1,60 @@
//
// monero_key_image_utils.hpp
// MyMonero
//
// Created by Paul Shapiro on 1/2/18.
// Copyright (c) 2014-2018, MyMonero.com
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 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.
//
// 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.
//
// 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.
//
//
#ifndef monero_key_image_utils_hpp
#define monero_key_image_utils_hpp
//
#include "crypto.h"
#include "cryptonote_basic.h"
//
using namespace tools;
#include "tools__ret_vals.hpp"
//
namespace monero_key_image_utils
{
struct KeyImageRetVals: RetVals_base
{
crypto::key_image calculated_key_image;
};
bool new__key_image(
const crypto::public_key& account_pub_spend_key,
const crypto::secret_key& account_sec_spend_key,
const crypto::secret_key& account_sec_view_key,
const crypto::public_key& tx_public_key,
uint64_t out_index,
KeyImageRetVals &KeyImageRetVals
);
}
//
#endif /* monero_key_image_utils_hpp */

@ -42,6 +42,10 @@ crypto::hash8 monero_paymentID_utils::new_short_plain_paymentID()
{
return crypto::rand<crypto::hash8>();
}
std::string monero_paymentID_utils::new_short_plain_paymentID_string()
{
return string_tools::pod_to_hex(monero_paymentID_utils::new_short_plain_paymentID());
}
//
bool monero_paymentID_utils::parse_long_payment_id(const std::string& payment_id_str, crypto::hash& payment_id)
{

@ -41,6 +41,7 @@ namespace monero_paymentID_utils
//
// Generating Payment IDs
crypto::hash8 new_short_plain_paymentID(); // This is favored - its length will be detected and encrypted automatically on send
std::string new_short_plain_paymentID_string();
//
// Parsing and Detecting Payment IDs
bool parse_long_payment_id(const std::string& payment_id_str, crypto::hash& payment_id);

@ -0,0 +1,233 @@
//
// monero_transfer_utils.cpp
// Copyright © 2018 MyMonero. All rights reserved.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 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.
//
// 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.
//
// 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.
//
//
//
//
#include "monero_transfer_utils.hpp"
//#include "monero_fork_rules.hpp"
//#include "wallet_errors.h
//
//using namespace std;
//using namespace crypto;
//using namespace epee;
//using namespace cryptonote;
//using namespace tools; // for error::
using namespace monero_transfer_utils;
//using namespace monero_fork_rules;
//
//
// monero_transfer_utils - General functions
uint64_t monero_transfer_utils::get_upper_transaction_size_limit(uint64_t upper_transaction_size_limit__or_0_for_default, use_fork_rules_fn_type use_fork_rules_fn)
{
if (upper_transaction_size_limit__or_0_for_default > 0)
return upper_transaction_size_limit__or_0_for_default;
uint64_t full_reward_zone = use_fork_rules_fn(5, 10) ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V5 : use_fork_rules_fn(2, 10) ? CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V2 : CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE_V1;
return full_reward_zone - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
}
uint64_t monero_transfer_utils::get_fee_multiplier(
uint32_t priority,
uint32_t default_priority,
int fee_algorithm,
use_fork_rules_fn_type use_fork_rules_fn
) {
static const uint64_t old_multipliers[3] = {1, 2, 3};
static const uint64_t new_multipliers[3] = {1, 20, 166};
static const uint64_t newer_multipliers[4] = {1, 4, 20, 166};
if (fee_algorithm == -1)
fee_algorithm = get_fee_algorithm(use_fork_rules_fn);
// 0 -> default (here, x1 till fee algorithm 2, x4 from it)
if (priority == 0)
priority = default_priority;
if (priority == 0)
{
if (fee_algorithm >= 2)
priority = 2;
else
priority = 1;
}
// 1 to 3/4 are allowed as priorities
uint32_t max_priority = (fee_algorithm >= 2) ? 4 : 3;
if (priority >= 1 && priority <= max_priority)
{
switch (fee_algorithm)
{
case 0: return old_multipliers[priority-1];
case 1: return new_multipliers[priority-1];
case 2: return newer_multipliers[priority-1];
default: THROW_WALLET_EXCEPTION_IF (true, error::invalid_priority);
}
}
THROW_WALLET_EXCEPTION_IF (false, error::invalid_priority);
return 1;
}
//
int monero_transfer_utils::get_fee_algorithm(use_fork_rules_fn_type use_fork_rules_fn)
{
// changes at v3 and v5
if (use_fork_rules_fn(5, 0))
return 2;
if (use_fork_rules_fn(3, -720 * 14))
return 1;
return 0;
}
//
size_t monero_transfer_utils::estimate_rct_tx_size(int n_inputs, int mixin, int n_outputs, size_t extra_size, bool bulletproof)
{
size_t size = 0;
// tx prefix
// first few bytes
size += 1 + 6;
// vin
size += n_inputs * (1+6+(mixin+1)*2+32);
// vout
size += n_outputs * (6+32);
// extra
size += extra_size;
// rct signatures
// type
size += 1;
// rangeSigs
if (bulletproof)
size += ((2*6 + 4 + 5)*32 + 3) * n_outputs;
else
size += (2*64*32+32+64*32) * n_outputs;
// MGs
size += n_inputs * (64 * (mixin+1) + 32);
// mixRing - not serialized, can be reconstructed
/* size += 2 * 32 * (mixin+1) * n_inputs; */
// pseudoOuts
size += 32 * n_inputs;
// ecdhInfo
size += 2 * 32 * n_outputs;
// outPk - only commitment is saved
size += 32 * n_outputs;
// txnFee
size += 4;
LOG_PRINT_L2("estimated rct tx size for " << n_inputs << " with ring size " << (mixin+1) << " and " << n_outputs << ": " << size << " (" << ((32 * n_inputs/*+1*/) + 2 * 32 * (mixin+1) * n_inputs + 32 * n_outputs) << " saved)");
return size;
}
size_t monero_transfer_utils::estimate_tx_size(bool use_rct, int n_inputs, int mixin, int n_outputs, size_t extra_size, bool bulletproof)
{
if (use_rct)
return estimate_rct_tx_size(n_inputs, mixin, n_outputs + 1, extra_size, bulletproof);
else
return n_inputs * (mixin+1) * APPROXIMATE_INPUT_BYTES + extra_size;
}
//
uint64_t monero_transfer_utils::calculate_fee(uint64_t fee_per_kb, size_t bytes, uint64_t fee_multiplier)
{
uint64_t kB = (bytes + 1023) / 1024;
return kB * fee_per_kb * fee_multiplier;
}
uint64_t monero_transfer_utils::calculate_fee(uint64_t fee_per_kb, const cryptonote::blobdata &blob, uint64_t fee_multiplier)
{
return calculate_fee(fee_per_kb, blob.size(), fee_multiplier);
}
//
// Transfer parsing/derived properties
bool monero_transfer_utils::is_transfer_unlocked(
uint64_t unlock_time,
uint64_t block_height,
uint64_t blockchain_size, /* extracting wallet2->m_blockchain.size() / m_local_bc_height */
bool is_testnet
) {
if(!is_tx_spendtime_unlocked(unlock_time, block_height, blockchain_size, is_testnet))
return false;
if(block_height + CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE > blockchain_size)
return false;
return true;
}
bool monero_transfer_utils::is_tx_spendtime_unlocked(
uint64_t unlock_time,
uint64_t block_height,
uint64_t blockchain_size,
bool is_testnet
) {
if(unlock_time < CRYPTONOTE_MAX_BLOCK_NUMBER)
{
//interpret as block index
if(block_height-1 + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS >= unlock_time)
return true;
else
return false;
}else
{
//interpret as time
uint64_t current_time = static_cast<uint64_t>(time(NULL));
// XXX: this needs to be fast, so we'd need to get the starting heights
// from the daemon to be correct once voting kicks in
uint64_t v2height = is_testnet ? 624634 : 1009827;
uint64_t leeway = block_height < v2height ? CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1 : CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V2;
if(current_time + leeway >= unlock_time)
return true;
else
return false;
}
return false;
}
uint32_t monero_transfer_utils::fixed_ringsize()
{
return 7; // TODO/FIXME: temporary…… for lightwallet code!
}
uint32_t monero_transfer_utils::fixed_mixinsize()
{
return monero_transfer_utils::fixed_ringsize() - 1;
}
std::string monero_transfer_utils::new_dummy_address_string_for_rct_tx(bool isTestnet)
{
cryptonote::account_base account;
account.generate();
//
return account.get_public_address_str(isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET);
}
uint32_t monero_transfer_utils::default_priority()
{
return 1; // lowest (TODO: declare)
}

@ -0,0 +1,75 @@
//
// monero_transfer_utils.hpp
// Copyright (c) 2014-2018, MyMonero.com
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 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.
//
// 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.
//
// 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.
//
//
#ifndef monero_transfer_utils_hpp
#define monero_transfer_utils_hpp
//
#include "string_tools.h"
#include "crypto.h"
#include "cryptonote_basic.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_tx_utils.h"
//
using namespace tools;
#include "tools__ret_vals.hpp"
//
// used to choose when to stop adding outputs to a tx
#define APPROXIMATE_INPUT_BYTES 80
// used to target a given block size (additional outputs may be added on top to build fee)
#define TX_SIZE_TARGET(bytes) (bytes*2/3)
//
namespace monero_transfer_utils
{
//
typedef std::function<bool(uint8_t, int64_t)> use_fork_rules_fn_type;
//
uint64_t get_upper_transaction_size_limit(uint64_t upper_transaction_size_limit__or_0_for_default, use_fork_rules_fn_type use_fork_rules_fn);
uint64_t get_fee_multiplier(uint32_t priority, uint32_t default_priority, int fee_algorithm, use_fork_rules_fn_type use_fork_rules_fn);
int get_fee_algorithm(use_fork_rules_fn_type use_fork_rules_fn);
//
uint64_t calculate_fee(uint64_t fee_per_kb, size_t bytes, uint64_t fee_multiplier);
uint64_t calculate_fee(uint64_t fee_per_kb, const cryptonote::blobdata &blob, uint64_t fee_multiplier);
//
size_t estimate_rct_tx_size(int n_inputs, int mixin, int n_outputs, size_t extra_size, bool bulletproof);
size_t estimate_tx_size(bool use_rct, int n_inputs, int mixin, int n_outputs, size_t extra_size, bool bulletproof);
//
bool is_transfer_unlocked(uint64_t unlock_time, uint64_t block_height, uint64_t blockchain_size, bool is_testnet = false);
bool is_tx_spendtime_unlocked(uint64_t unlock_time, uint64_t block_height, uint64_t blockchain_size, bool is_testnet = false);
//
uint32_t fixed_ringsize(); // not mixinsize, which would be ringsize-1
uint32_t fixed_mixinsize(); // not ringsize, which would be mixinsize+1
//
std::string new_dummy_address_string_for_rct_tx(bool isTestnet = false);
//
uint32_t default_priority();
}
#endif /* monero_transfer_utils_hpp */

@ -32,7 +32,10 @@
//
#include "monero_wallet_utils.hpp"
#include <boost/algorithm/string.hpp>
#include "cryptonote_basic.h"
#include "device/device.hpp"
#include "cryptonote_basic/account.h"
#include "keccak.h"
//
#include "string_tools.h"
using namespace epee;
@ -44,19 +47,63 @@ extern "C" {
using namespace monero_wallet_utils;
using namespace crypto; // for extension
//
// 16 byte seeds
void cn_pad_by_fast_hash__C(const uint8_t *in, size_t inlen, uint8_t *md, int mdlen)
{
keccak(in, inlen, md, mdlen);
}
inline void cn_pad_by_fast_hash(const uint8_t *indata, std::size_t inlen, uint8_t *outdata, std::size_t outlen)
{
cn_pad_by_fast_hash__C(indata, inlen, outdata, (int)outlen);
}
void monero_wallet_utils::coerce_valid_sec_key_from(
const legacy16B_secret_key &legacy16B_mymonero_sec_seed,
secret_key &dst__sec_seed
) { // cn_fast_hash legacy16B_sec_seed in order to 'pad' it to 256 bits so it can be chopped to ec_scalar
static_assert(!epee::has_padding<legacy16B_secret_key>(), "potential hash of padding data");
static_assert(!epee::has_padding<secret_key>(), "writing to struct with extra data");
cn_pad_by_fast_hash((uint8_t *)&legacy16B_mymonero_sec_seed, sizeof(legacy16B_secret_key),
(uint8_t *)&dst__sec_seed, sizeof(secret_key));
}
bool monero_wallet_utils::words_to_bytes(
std::string words,
legacy16B_secret_key& dst,
std::string &language_name
) {
std::string s;
if (!crypto::ElectrumWords::words_to_bytes(words, s, sizeof(dst), true, language_name)) {
return false;
}
if (s.size() != sizeof(dst)) {
return false;
}
memcpy(dst.data, s.data(), sizeof(dst.data));
return true;
}
bool monero_wallet_utils::bytes_to_words(
const legacy16B_secret_key& src,
std::string& words,
const std::string &language_name
) {
return crypto::ElectrumWords::bytes_to_words(
src.data, sizeof(src),
words, language_name
);
}
//
//
bool monero_wallet_utils::new_wallet(
std::string mnemonic_language,
WalletDescriptionRetVals &retVals,
bool isTestnet
)
{
) {
retVals = {};
//
cryptonote::account_base account{}; // this initializes the wallet and should call the default constructor
crypto::secret_key nonLegacy32B_sec_seed = account.generate();
//
const cryptonote::account_keys& keys = account.get_keys();
std::string address_string = account.get_public_address_str(isTestnet); // getting the string here instead of leaving it to the consumer b/c get_public_address_str could potentially change in implementation (see TODO) so it's not right to duplicate that here
std::string address_string = account.get_public_address_str(isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET); // getting the string here instead of leaving it to the consumer b/c get_public_address_str could potentially change in implementation (see TODO) so it's not right to duplicate that here
//
std::string mnemonic_string;
bool r = crypto::ElectrumWords::bytes_to_words(nonLegacy32B_sec_seed, mnemonic_string, std::move(mnemonic_language));
@ -68,8 +115,7 @@ bool monero_wallet_utils::new_wallet(
//
return false;
}
retVals.optl__desc =
{
retVals.optl__desc = WalletDescription{
string_tools::pod_to_hex(nonLegacy32B_sec_seed),
//
address_string,
@ -84,6 +130,9 @@ bool monero_wallet_utils::new_wallet(
return true;
}
//
const uint32_t stable_32B_seed_mnemonic_word_count = 25;
const uint32_t legacy_16B_seed_mnemonic_word_count = 13;
bool monero_wallet_utils::decoded_seed(
std::string mnemonic_string,
std::string mnemonic_language,
@ -108,7 +157,7 @@ bool monero_wallet_utils::decoded_seed(
crypto::secret_key sec_seed;
std::string sec_seed_string; // TODO/FIXME: needed this for shared ref outside of if branch below… not intending extra default constructor call but not sure how to get around it yet
bool from_legacy16B_lw_seed = false;
if (word_count == crypto::ElectrumWords::stable_32B_seed_mnemonic_word_count) {
if (word_count == stable_32B_seed_mnemonic_word_count) {
from_legacy16B_lw_seed = false; // to be clear
bool r = crypto::ElectrumWords::words_to_bytes(mnemonic_string, sec_seed, mnemonic_language);
if (!r) {
@ -118,17 +167,17 @@ bool monero_wallet_utils::decoded_seed(
return false;
}
sec_seed_string = string_tools::pod_to_hex(sec_seed);
} else if (word_count == crypto::ElectrumWords::legacy_16B_seed_mnemonic_word_count) {
} else if (word_count == legacy_16B_seed_mnemonic_word_count) {
from_legacy16B_lw_seed = true;
crypto::legacy16B_secret_key legacy16B_sec_seed;
bool r = crypto::ElectrumWords::words_to_bytes(mnemonic_string, legacy16B_sec_seed, mnemonic_language); // special 16 byte function
legacy16B_secret_key legacy16B_sec_seed;
bool r = words_to_bytes(mnemonic_string, legacy16B_sec_seed, mnemonic_language); // special 16 byte function
if (!r) {
retVals.did_error = true;
retVals.err_string = "Invalid 13-word mnemonic";
//
return false;
}
crypto::coerce_valid_sec_key_from(legacy16B_sec_seed, sec_seed);
coerce_valid_sec_key_from(legacy16B_sec_seed, sec_seed);
sec_seed_string = string_tools::pod_to_hex(legacy16B_sec_seed); // <- NOTE: we are returning the _LEGACY_ seed as the string… this is important so we don't lose the fact it was 16B/13-word originally!
} else {
retVals.did_error = true;
@ -144,6 +193,48 @@ bool monero_wallet_utils::decoded_seed(
return true;
}
//
SeedDecodedMnemonic_RetVals monero_wallet_utils::mnemonic_string_from_seed_hex_string(
std::string sec_hexString,
std::string mnemonic_language // aka wordset name
) {
SeedDecodedMnemonic_RetVals retVals = {};
//
// std::string mnemonic_string;
// uint32_t sec_hexString_length = sec_hexString.size();
// //
// bool r = false;
// if (sec_hexString_length == sec_seed_hex_string_length) { // normal seed
// crypto::secret_key sec_seed;
// r = string_tools::hex_to_pod(sec_hexString, sec_seed);
// if (!r) {
// retVals.did_error = true;
// retVals.err_string = "Invalid seed";
// return retVals;
// }
// r = crypto::ElectrumWords::bytes_to_words(sec_seed, mnemonic_string, mnemonic_language);
// } else if (sec_hexString_length == legacy16B__sec_seed_hex_string_length) {
// legacy16B_secret_key legacy16B_sec_seed;
// r = string_tools::hex_to_pod(sec_hexString, legacy16B_sec_seed);
// if (!r) {
// retVals.did_error = true;
// retVals.err_string = "Invalid seed";
// return retVals;
// }
// r = bytes_to_words(legacy16B_sec_seed, mnemonic_string, mnemonic_language); // called with the legacy16B version
// } else {
// retVals.did_error = true;
// retVals.err_string = "Invalid seed length";
// return retVals;
// }
// if (!r) {
// retVals.did_error = true;
// retVals.err_string = "Couldn't get mnemonic from hex seed";
// return retVals;
// }
// retVals.mnemonic_string = mnemonic_string; // TODO: should/can we just send retVals.mnemonic_string to bytes_to_words ?
return retVals;
}
//
bool monero_wallet_utils::wallet_with(
std::string mnemonic_string,
std::string mnemonic_language,
@ -167,11 +258,10 @@ bool monero_wallet_utils::wallet_with(
decodedSeed_retVals.from_legacy16B_lw_seed // assumed set if r
);
const cryptonote::account_keys& keys = account.get_keys();
retVals.optl__desc =
{
retVals.optl__desc = WalletDescription{
*decodedSeed_retVals.optl__sec_seed_string, // assumed non nil if r
//
account.get_public_address_str(isTestnet),
account.get_public_address_str(isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET),
//
keys.m_spend_secret_key,
keys.m_view_secret_key,
@ -195,7 +285,7 @@ bool monero_wallet_utils::validate_wallet_components_with(
cryptonote::address_parse_info decoded_address_info;
r = cryptonote::get_account_address_from_str(
decoded_address_info,
inputs.isTestnet,
inputs.isTestnet ? cryptonote::TESTNET : cryptonote::MAINNET,
inputs.address_string
);
if (r == false) {
@ -267,7 +357,7 @@ bool monero_wallet_utils::validate_wallet_components_with(
unsigned long sec_seed_string_length = (*inputs.optl__sec_seed_string).length();
crypto::secret_key sec_seed;
bool from_legacy16B_lw_seed = false;
if (sec_seed_string_length == crypto::sec_seed_hex_string_length) { // normal seed
if (sec_seed_string_length == sec_seed_hex_string_length) { // normal seed
from_legacy16B_lw_seed = false; // to be clear
bool r = string_tools::hex_to_pod((*inputs.optl__sec_seed_string), sec_seed);
if (!r) {
@ -276,9 +366,9 @@ bool monero_wallet_utils::validate_wallet_components_with(
//
return false;
}
} else if (sec_seed_string_length == crypto::legacy16B__sec_seed_hex_string_length) {
} else if (sec_seed_string_length == legacy16B__sec_seed_hex_string_length) {
from_legacy16B_lw_seed = true;
crypto::legacy16B_secret_key legacy16B_sec_seed;
legacy16B_secret_key legacy16B_sec_seed;
bool r = string_tools::hex_to_pod((*inputs.optl__sec_seed_string), legacy16B_sec_seed);
if (!r) {
outputs.did_error = true;
@ -286,7 +376,7 @@ bool monero_wallet_utils::validate_wallet_components_with(
//
return false;
}
crypto::coerce_valid_sec_key_from(legacy16B_sec_seed, sec_seed);
coerce_valid_sec_key_from(legacy16B_sec_seed, sec_seed);
}
cryptonote::account_base expected_account{}; // this initializes the wallet and should call the default constructor
expected_account.generate(sec_seed, true/*recover*/, false/*two_random*/, from_legacy16B_lw_seed);

@ -34,9 +34,10 @@
#define monero_wallet_utils_hpp
#include <boost/optional.hpp>
#include "crypto.h"
#include "serialization/binary_archive.h"
#include "cryptonote_basic.h"
#include "cryptonote_basic_impl.h"
#include "electrum-words.h"
#include "mnemonics/singleton.h"
#include "mnemonics/english.h"
@ -46,6 +47,28 @@ using namespace tools;
//
namespace monero_wallet_utils
{
//
// 16B keys
POD_CLASS ec_nonscalar_16Byte {
// extension to support old deprecated 16B/13-word seeds
char data[16];
};
using legacy16B_secret_key = tools::scrubbed<ec_nonscalar_16Byte>;
void coerce_valid_sec_key_from(
const legacy16B_secret_key &legacy16B_mymonero_sec_seed,
crypto::secret_key &dst__sec_seed
);
static_assert(sizeof(legacy16B_secret_key) == 16, "Invalid structure size");
inline std::ostream &operator <<(std::ostream &o, const legacy16B_secret_key &v) {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
const static legacy16B_secret_key null_legacy16B_skey = boost::value_initialized<legacy16B_secret_key>();
const static unsigned long sec_seed_hex_string_length = sizeof(crypto::secret_key) * 2;
const static unsigned long legacy16B__sec_seed_hex_string_length = sizeof(legacy16B_secret_key) * 2;
//
bool words_to_bytes(std::string words, legacy16B_secret_key& dst, std::string &language_name);
bool bytes_to_words(const legacy16B_secret_key& src, std::string& words, const std::string &language_name);
//
//
// Accounts
struct MnemonicDecodedSeed_RetVals: RetVals_base
@ -62,6 +85,12 @@ namespace monero_wallet_utils
MnemonicDecodedSeed_RetVals &retVals
);
//
struct SeedDecodedMnemonic_RetVals: RetVals_base
{
boost::optional<std::string> mnemonic_string = boost::none;
};
SeedDecodedMnemonic_RetVals mnemonic_string_from_seed_hex_string(std::string seed_string, std::string wordsetName);
//
// Convenience functions - Wallets
struct WalletDescription
{
@ -113,4 +142,34 @@ namespace monero_wallet_utils
);
}
#define MONEROWALLETUTILS_MAKE_COMPARABLE(type) \
namespace monero_wallet_utils { \
inline bool operator==(const type &_v1, const type &_v2) { \
return std::memcmp(&_v1, &_v2, sizeof(type)) == 0; \
} \
inline bool operator!=(const type &_v1, const type &_v2) { \
return std::memcmp(&_v1, &_v2, sizeof(type)) != 0; \
} \
}
#define MONEROWALLETUTILS_MAKE_HASHABLE(type) \
MONEROWALLETUTILS_MAKE_COMPARABLE(type) \
namespace monero_wallet_utils { \
static_assert(sizeof(std::size_t) <= sizeof(type), "Size of " #type " must be at least that of size_t"); \
inline std::size_t hash_value(const type &_v) { \
return reinterpret_cast<const std::size_t &>(_v); \
} \
} \
namespace std { \
template<> \
struct hash<monero_wallet_utils::type> { \
std::size_t operator()(const monero_wallet_utils::type &_v) const { \
return reinterpret_cast<const std::size_t &>(_v); \
} \
}; \
}
MONEROWALLETUTILS_MAKE_HASHABLE(legacy16B_secret_key)
#endif /* monero_wallet_utils_hpp */

@ -41,7 +41,7 @@ namespace tools
struct RetVals_base
{
bool did_error = false;
optional<string> err_str = none;
optional<string> err_string = none;
//
// derive *_RetVals structs from this type and add your own members
};

@ -41,6 +41,7 @@
#include <iterator>
#include <sstream>
using namespace std;
#include "string_tools.h"
// using namespace boost;
//
// Shared code
@ -51,9 +52,9 @@ BOOST_AUTO_TEST_CASE(decodeAddress)
{
string address = "43zxvpcj5Xv9SEkNXbMCG7LPQStHMpFCQCmkmR4u5nzjWwq5Xkv5VmGgYEsHXg4ja2FGRD5wMWbBVMijDTqmmVqm93wHGkg";
auto result = monero::address_utils::decodedAddress(address, false);
if (result.err_str) {
std::cout << *result.err_str << endl;
BOOST_REQUIRE(!result.err_str);
if (result.err_string) {
std::cout << *result.err_string << endl;
BOOST_REQUIRE(!result.err_string);
}
BOOST_REQUIRE(result.pub_viewKey_string != none);
BOOST_REQUIRE(result.pub_spendKey_string != none);
@ -61,3 +62,31 @@ BOOST_AUTO_TEST_CASE(decodeAddress)
BOOST_REQUIRE(result.paymentID_string == none);
std::cout << "Decoded: " << address << std::endl;
}
//
//
#include "../src/monero_paymentID_utils.hpp"
BOOST_AUTO_TEST_CASE(paymentID)
{
string paymentID_string = monero_paymentID_utils::new_short_plain_paymentID_string();
std::cout << "paymentID_string: " << paymentID_string << std::endl;
BOOST_REQUIRE_MESSAGE(paymentID_string.size() == 16, "Expected payment ID to be of length 16");
//
crypto::hash parsed__payment_id;
bool didParse = monero_paymentID_utils::parse_payment_id(paymentID_string, parsed__payment_id);
BOOST_REQUIRE_MESSAGE(didParse, "Couldn't parse payment ID");
std::string parsed__payment_id_as_string = epee::string_tools::pod_to_hex(parsed__payment_id);
BOOST_REQUIRE_MESSAGE(paymentID_string.compare(parsed__payment_id_as_string), "Expected parsed payment ID to equal original payment ID");
std::cout << "parsed__payment_id: " << parsed__payment_id << std::endl;
}
//
//
#include "../src/monero_key_image_utils.hpp"
BOOST_AUTO_TEST_CASE(keyImage)
{
}
//
//
#include "../src/monero_wallet_utils.hpp"
BOOST_AUTO_TEST_CASE(wallet)
{
}

Loading…
Cancel
Save