BitShares Forum

Main => Technical Support => Topic started by: jamesc on May 02, 2014, 04:09:51 pm

Title: Handling keys (API)
Post 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";
Title: Re: Handling keys (API)
Post by: bytemaster on May 02, 2014, 05:20:38 pm
std::string( fc::ecc::private_key::regenerate( fc::sha256( "HEXSTRING" ) ).get_public_key() )
Title: Re: Handling keys (API)
Post by: jamesc on May 02, 2014, 06:02:27 pm
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)));

Title: Re: Handling keys (API)
Post by: bytemaster on May 02, 2014, 06:14:31 pm
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();
Title: Re: Handling keys (API)
Post by: bytemaster on May 02, 2014, 06:15:14 pm
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...
Title: Re: Handling keys (API)
Post by: bytemaster on May 02, 2014, 06:16:37 pm
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.
Title: Re: Handling keys (API)
Post by: jamesc on May 02, 2014, 06:56:04 pm
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";
Title: Re: Handling keys (API)
Post by: bytemaster on May 02, 2014, 07:07:14 pm


Simple approach here:
std::cout << "public b58:\t" << k2.get_public_key().to_base58()<< "\n";
Title: Re: Handling keys (API)
Post by: jamesc on May 06, 2014, 08:54:46 pm
New question ... If I run this In address.cpp std::string, why do I get two different encodings?
Code: [Select]
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.
Title: Re: Handling keys (API)
Post by: toast on May 06, 2014, 09:49:56 pm
Maybe copy from addr.data instead of addr

Sent from my SCH-I535 using Tapatalk

Title: Re: Handling keys (API)
Post by: toast on May 06, 2014, 09:50:43 pm
 also sizeo(addr.data)

Sent from my SCH-I535 using Tapatalk

Title: Re: Handling keys (API)
Post by: jamesc on May 08, 2014, 04:34:37 pm
I figured it out.. bin_addr is 4 bytes longer than addr...