Skip to content

Commit

Permalink
Merge pull request #73 from GeniusVentures/optional-private-key
Browse files Browse the repository at this point in the history
Optional private key
  • Loading branch information
henriqueaklein authored Dec 5, 2024
2 parents fb655fa + d15c462 commit 4f08fe6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
105 changes: 56 additions & 49 deletions src/account/GeniusAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,77 @@
#include "singleton/CComponentFactory.hpp"
#include <nil/crypto3/pubkey/algorithm/sign.hpp>

sgns::GeniusAccount::GeniusAccount( const uint8_t token_type,
std::string_view base_path,
std::string_view eth_private_key ) :
token( token_type ), nonce( 0 ), balance( 0 )
namespace sgns
{
if ( auto maybe_address = GenerateGeniusAddress( base_path, eth_private_key ); maybe_address.has_value() )
GeniusAccount::GeniusAccount( const uint8_t token_type, std::string_view base_path, const char *eth_private_key ) :
token( token_type ), nonce( 0 ), balance( 0 )
{
address = maybe_address.value();
}
else
{
std::cerr << "Error Is? " << maybe_address.error().message() << std::endl;
throw std::runtime_error( "Could not generate wallet address" );
if ( auto maybe_address = GenerateGeniusAddress( base_path, eth_private_key ); maybe_address.has_value() )
{
address = maybe_address.value();
}
else
{
std::cerr << "Could not get SGNS address: " << maybe_address.error().message() << std::endl;
throw std::runtime_error( maybe_address.error().message() );
}

//TODO - Retrieve values where? Read through blockchain Here?
// How to deal with errors?
}

//TODO - Retrieve values where? Read through blockchain Here?
// How to deal with errors?
}
outcome::result<KeyGenerator::ElGamal> GeniusAccount::GenerateGeniusAddress( std::string_view base_path,
const char *eth_private_key )
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
OUTCOME_TRY( ( auto &&, icomponent ), component_factory->GetComponent( "LocalSecureStorage" ) );

outcome::result<KeyGenerator::ElGamal> sgns::GeniusAccount::GenerateGeniusAddress( std::string_view base_path,
std::string_view eth_private_key )
{
auto component_factory = SINGLETONINSTANCE( CComponentFactory );
OUTCOME_TRY( ( auto &&, icomponent ), component_factory->GetComponent( "LocalSecureStorage" ) );
auto secure_storage = std::dynamic_pointer_cast<ISecureStorage>( icomponent );
auto load_res = secure_storage->Load( "sgns_key", std::string( base_path ) );

auto secure_storage = std::dynamic_pointer_cast<ISecureStorage>( icomponent );
auto load_res = secure_storage->Load( "sgns_key", std::string( base_path ) );
nil::crypto3::multiprecision::uint256_t key_seed;
if ( load_res )
{
key_seed = nil::crypto3::multiprecision::uint256_t( load_res.value() );
}
else
{
if ( eth_private_key == nullptr )
{
return outcome::failure( std::errc::invalid_argument );
}

nil::crypto3::multiprecision::uint256_t key_seed;
if ( load_res )
{
key_seed = nil::crypto3::multiprecision::uint256_t( load_res.value() );
}
else
{
ethereum::EthereumKeyGenerator private_key( eth_private_key );
ethereum::EthereumKeyGenerator private_key( eth_private_key );

KeyGenerator::ElGamal elgamal_pubkey_predefined( 0x1234_cppui256 );
KeyGenerator::ElGamal elgamal_pubkey_predefined( 0x1234_cppui256 );

auto fixed_public_key = elgamal_pubkey_predefined.GetPublicKey().public_key_value.str();
auto fixed_public_key = elgamal_pubkey_predefined.GetPublicKey().public_key_value.str();

nil::crypto3::pubkey::public_key<ethereum::policy_type>::signature_type signed_secret =
nil::crypto3::sign<ethereum::policy_type>( fixed_public_key, private_key.get_private_key() );
nil::crypto3::pubkey::public_key<ethereum::policy_type>::signature_type signed_secret =
nil::crypto3::sign<ethereum::policy_type>( fixed_public_key, private_key.get_private_key() );

std::vector<std::uint8_t> signed_vector( 64 );
std::vector<std::uint8_t> signed_vector( 64 );

nil::marshalling::bincode::field<ecdsa_t::scalar_field_type>::field_element_to_bytes<
std::vector<std::uint8_t>::iterator>( std::get<0>( signed_secret ),
signed_vector.begin(),
signed_vector.begin() + signed_vector.size() / 2 );
nil::marshalling::bincode::field<ecdsa_t::scalar_field_type>::field_element_to_bytes<
std::vector<std::uint8_t>::iterator>( std::get<1>( signed_secret ),
signed_vector.begin() + signed_vector.size() / 2,
signed_vector.end() );
nil::marshalling::bincode::field<ecdsa_t::scalar_field_type>::field_element_to_bytes<
std::vector<std::uint8_t>::iterator>( std::get<0>( signed_secret ),
signed_vector.begin(),
signed_vector.begin() + signed_vector.size() / 2 );
nil::marshalling::bincode::field<ecdsa_t::scalar_field_type>::field_element_to_bytes<
std::vector<std::uint8_t>::iterator>( std::get<1>( signed_secret ),
signed_vector.begin() + signed_vector.size() / 2,
signed_vector.end() );

std::array<uint8_t, 32> hashed = nil::crypto3::hash<ecdsa_t::hashes::sha2<256>>( signed_vector.cbegin(),
signed_vector.cend() );
std::array<uint8_t, 32> hashed = nil::crypto3::hash<ecdsa_t::hashes::sha2<256>>( signed_vector.cbegin(),
signed_vector.cend() );

key_seed = nil::crypto3::multiprecision::uint256_t( hashed );
key_seed = nil::crypto3::multiprecision::uint256_t( hashed );

BOOST_OUTCOME_TRYV2( auto &&, secure_storage->Save( "sgns_key", key_seed.str(), std::string( base_path ) ) );
}
BOOST_OUTCOME_TRYV2( auto &&,
secure_storage->Save( "sgns_key", key_seed.str(), std::string( base_path ) ) );
}

KeyGenerator::ElGamal sgnus_key( key_seed );
KeyGenerator::ElGamal sgnus_key( key_seed );

return sgnus_key;
return sgnus_key;
}
}
4 changes: 2 additions & 2 deletions src/account/GeniusAccount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace sgns
class GeniusAccount
{
public:
GeniusAccount( uint8_t token_type, std::string_view base_path, std::string_view eth_private_key );
GeniusAccount( uint8_t token_type, std::string_view base_path, const char *eth_private_key );

~GeniusAccount()
{
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace sgns
uint64_t balance;

static outcome::result<KeyGenerator::ElGamal> GenerateGeniusAddress( std::string_view base_path,
std::string_view eth_private_key );
const char *eth_private_key );
};
}

Expand Down

0 comments on commit 4f08fe6

Please sign in to comment.