Skip to content

Commit

Permalink
Add helper functions for generating address from separate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
who-biz committed Jul 14, 2019
1 parent d435c5d commit 1d4397c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cryptonote_basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ target_link_libraries(cryptonote_basic
cncrypto
checkpoints
device
hydrogen
${Boost_DATE_TIME_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
Expand Down
39 changes: 38 additions & 1 deletion src/cryptonote_basic/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers

#define context "accounts"
#define hydro_kdf_KEYBYTES 32
#include <fstream>

#include "include_base_utils.h"
#include "account.h"
#include "warnings.h"
#include "crypto/crypto.h"
#include "string_tools.h"
extern "C"
{
#include "crypto/keccak.h"
}
#include "cryptonote_basic_impl.h"
#include "cryptonote_format_utils.h"
#include "libhydrogen/hydrogen.h"

#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "account"
Expand Down Expand Up @@ -78,6 +82,31 @@ DISABLE_VS_WARNINGS(4244 4345)
m_keys.m_multisig_keys.clear();
}
//-----------------------------------------------------------------
crypto::secret_key account_base::uchar_array_to_key(uint8_t array[hydro_kdf_KEYBYTES])
{
std::ostringstream convert;
for (int a = 0; a < hydro_kdf_KEYBYTES; a++) {
convert << array[a];
}
std::string secret = convert.str();
crypto::secret_key ret;
bool r = epee::string_tools::hex_to_pod(secret, ret);
if(!r)
return crypto::null_skey;
return ret;
}
//-----------------------------------------------------------------
crypto::secret_key account_base::generate_secret()
{
if (hydro_init() != 0)
abort();

uint8_t secret_key[hydro_kdf_KEYBYTES];
hydro_kdf_keygen(secret_key);
crypto::secret_key ret = uchar_array_to_key(secret_key);
return ret;
}
//-----------------------------------------------------------------
crypto::secret_key account_base::generate(const crypto::secret_key& recovery_key, bool recover, bool two_random)
{
crypto::secret_key first = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, recovery_key, recover);
Expand Down Expand Up @@ -127,7 +156,15 @@ DISABLE_VS_WARNINGS(4244 4345)
if (m_creation_timestamp == (uint64_t)-1) // failure
m_creation_timestamp = 0; // lowest value
}

//-----------------------------------------------------------------
cryptonote::account_public_address account_base::create_from_btc(const crypto::secret_key& btc_pubkey, const crypto::secret_key& spendkey)
{
crypto::secret_key first = generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key, btc_pubkey, true);
crypto::secret_key second = generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key, spendkey, true);
cryptonote::account_public_address address;
create_from_keys(address, first, second);
return address;
}
//-----------------------------------------------------------------
void account_base::create_from_device(const std::string &device_name)
{
Expand Down
5 changes: 4 additions & 1 deletion src/cryptonote_basic/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ namespace cryptonote
public:
account_base();
crypto::secret_key generate(const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false);
void create_from_device(const std::string &device_name) ;
void create_from_keys(const cryptonote::account_public_address& address, const crypto::secret_key& spendkey, const crypto::secret_key& viewkey);
crypto::secret_key uchar_array_to_key(uint8_t key[32]);
cryptonote::account_public_address create_from_btc(const crypto::secret_key &btc_pubkey, const crypto::secret_key& spendkey);
crypto::secret_key generate_secret();
void create_from_device(const std::string &device_name) ;
void create_from_viewkey(const cryptonote::account_public_address& address, const crypto::secret_key& viewkey);
bool make_multisig(const crypto::secret_key &view_secret_key, const crypto::secret_key &spend_secret_key, const crypto::public_key &spend_public_key, const std::vector<crypto::secret_key> &multisig_keys);
void finalize_multisig(const crypto::public_key &spend_public_key);
Expand Down

0 comments on commit 1d4397c

Please sign in to comment.