Skip to content

Commit

Permalink
Example of sorting public keys #630
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Mar 22, 2016
1 parent 006d548 commit 38a0f3a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions programs/build_helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ endif()
# we only actually need Boost, but link against FC for now so we don't duplicate it.
target_link_libraries( member_enumerator PRIVATE fc graphene_app graphene_net graphene_chain graphene_egenesis_brief graphene_utilities graphene_wallet ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )

add_executable( sort-keys sort-keys.cpp )
if( UNIX AND NOT APPLE )
set(rt_library rt )
endif()

# we only actually need Boost, but link against FC for now so we don't duplicate it.
target_link_libraries( sort-keys PRIVATE fc graphene_chain graphene_egenesis_brief graphene_utilities ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )
47 changes: 47 additions & 0 deletions programs/build_helpers/sort-keys.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

#include <graphene/chain/protocol/address.hpp>
#include <graphene/chain/protocol/types.hpp>

#include <fc/crypto/elliptic.hpp>
#include <fc/io/json.hpp>

#include <algorithm>
#include <iostream>
#include <vector>

#define NUM_KEYS 100

int main( int argc, char** argv, char** envp )
{
std::vector< graphene::chain::public_key_type > pubkeys;
std::vector< graphene::chain::address > addresses;

for( int i=0; i<100; i++ )
{
std::string text = fc::to_string(i);
fc::ecc::private_key k = fc::ecc::private_key::regenerate( fc::sha256::hash( text ) );
pubkeys.emplace_back( k.get_public_key() );
addresses.emplace_back( pubkeys.back() );
}

std::sort( pubkeys.begin(), pubkeys.end() );
std::sort( addresses.begin(), addresses.end() );
std::cout << "list of sorted keys:" << std::endl << std::endl;
for( const auto& pub : pubkeys )
std::cout << std::string( pub ) << std::endl;
std::cout << std::endl << "list of sorted addresses:" << std::endl << std::endl;
for( const auto& addr : addresses )
std::cout << std::string( addr ) << std::endl;
std::cout << std::endl << "list of sorted keys with corresponding address / ripemd160:" << std::endl << std::endl;
for( const auto& pub : pubkeys )
{
fc::mutable_variant_object mvo;
graphene::chain::address addr = graphene::chain::address( pub );
mvo( "public_key", std::string( pub ) )
( "address", std::string( addr ) )
( "hex_address", addr.addr.str() )
;
std::cout << fc::json::to_string( mvo ) << std::endl;
}
return 0;
}
11 changes: 11 additions & 0 deletions tests/tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,15 @@ BOOST_AUTO_TEST_CASE( merkle_root )
BOOST_CHECK( block.calculate_merkle_root() == c(dO) );
}

BOOST_AUTO_TEST_CASE( public_key_ordering )
{
auto ka = generate_private_key("a");
auto kb = generate_private_key("b");

public_key_type pa = ka.get_public_key();
public_key_type pb = kb.get_public_key();

bool x = (pa < pb);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 38a0f3a

Please sign in to comment.