BitShares Forum
Main => Technical Support => Topic started by: jamesc on May 02, 2014, 04:09:51 pm
-
I would like to go from a private HEX key back into public keys. I'm not really a C coder, but here is my attempt. Is there a better way, perhaps a way that actually compiles? Or perhaps there is something already in the toolkit to do this.
EC_KEY* k= EC_KEY_new();
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1);
EC_KEY_set_group(k,group);
fc::sha256 sec=new fc::sha256 sec("5b5ca05c294f6778f84f0f5ea01f017a0c7e10fcf7a3cba6e67ae0e6318b389d");
//int nbytes = BN_num_bytes(bn);
//BN_hex2bn( &((unsigned char*)&sec)[32-nbytes], bn);
EC_KEY_set_private_key(k,sec);
std::cout << "bts address: "
<< std::string( bts::blockchain::address( k.get_public_key() ) ) <<"\n";
std::cout << "pts address: "
<< std::string( bts::blockchain::pts_address( k.get_public_key() ) ) <<"\n";
-
std::string( fc::ecc::private_key::regenerate( fc::sha256( "HEXSTRING" ) ).get_public_key() )
-
Works... Now, I need to see the fill public key in hex... Something like this:
auto k2 = fc::ecc::private_key::regenerate(key.get_secret());
auto pk2=k2.get_public_key().serialize();
std::cout << std::string(fc::to_hex(pk2.data,sizeof(pk2)));
-
Works... Now, I need to see the fill public key in hex... Something like this:
auto k2 = fc::ecc::private_key::regenerate(key.get_secret());
auto pk2=k2.get_public_key().serialize();
std::cout << std::string(fc::to_hex(pk2.data,sizeof(pk2.data)));
fc::variant( k2.get_public_key() ).as_string();
-
Works... Now, I need to see the fill public key in hex... Something like this:
auto k2 = fc::ecc::private_key::regenerate(key.get_secret());
auto pk2=k2.get_public_key().serialize();
std::cout << std::string(fc::to_hex(pk2.data,sizeof(pk2.data)));
fc::variant( k2.get_public_key() ).as_string();
Oh, that will give you base58...
-
Works... Now, I need to see the fill public key in hex... Something like this:
auto k2 = fc::ecc::private_key::regenerate(key.get_secret());
auto pk2=k2.get_public_key().serialize();
std::cout << std::string(fc::to_hex(pk2.data,sizeof(pk2.data)));
fc::variant( k2.get_public_key() ).as_string();
For hex:
fc::variant( k2.get_public_key().serialize() ).as_string();
Your approach also works.
-
This is what I ended up with:
std::cout << "public b58:\t" << fc::variant( k2.get_public_key().to_base58() ).as_string()<< "\n";
std::cout << "public hex:\t" << fc::variant( k2.get_public_key().serialize() ).as_string()<< "\n";
-
Simple approach here:
std::cout << "public b58:\t" << k2.get_public_key().to_base58()<< "\n";
-
New question ... If I run this In address.cpp std::string, why do I get two different encodings?
std::cout<<"bts 58\t"<<fc::to_base58(addr.data(), sizeof(addr))<<"\n";
memcpy( (char*)&bin_addr, (char*)&addr, sizeof(addr) );
std::cout<<"bin addr\t"<<fc::to_base58(bin_addr.data, sizeof(bin_addr))<<"\n";
EDIT: Both cout lines seem to work because I get hashes that match; a) 1st cout matches my javascript port of this code, b) 2nd cout matches bts_create_key.cpp (the BTS public key, the one the public uses)... I just don't know why the hashes are different. So I suspect the mem copy is doing something unexpected. The mem copy line is original.
-
Maybe copy from addr.data instead of addr
Sent from my SCH-I535 using Tapatalk
-
also sizeo(addr.data)
Sent from my SCH-I535 using Tapatalk
-
I figured it out.. bin_addr is 4 bytes longer than addr...