Author [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [EN] [ZH] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] Topic: Handling keys (API)  (Read 552 times)

0 Members and 1 Guest are viewing this topic.

Offline jamesc

Handling keys (API)
« 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";
« Last Edit: May 02, 2014, 04:38:42 PM by jcalfee1 »

Offline bytemaster

Re: Handling keys (API)
« Reply #1 on: May 02, 2014, 05:20:38 PM »
std::string( fc::ecc::private_key::regenerate( fc::sha256( "HEXSTRING" ) ).get_public_key() )
« Last Edit: May 02, 2014, 05:33:03 PM by bytemaster »
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline jamesc

Re: Handling keys (API)
« Reply #2 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)));

« Last Edit: May 02, 2014, 06:15:29 PM by jcalfee1 »

Offline bytemaster

Re: Handling keys (API)
« Reply #3 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();
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline bytemaster

Re: Handling keys (API)
« Reply #4 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...
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline bytemaster

Re: Handling keys (API)
« Reply #5 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.
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline jamesc

Re: Handling keys (API)
« Reply #6 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";

Offline bytemaster

Re: Handling keys (API)
« Reply #7 on: May 02, 2014, 07:07:14 PM »


Simple approach here:
std::cout << "public b58:\t" << k2.get_public_key().to_base58()<< "\n";
For the latest updates checkout my blog: http://bytemaster.bitshares.org
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

Offline jamesc

Re: Handling keys (API)
« Reply #8 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.
« Last Edit: May 06, 2014, 11:27:33 PM by jcalfee1 »

Offline toast

Re: Handling keys (API)
« Reply #9 on: May 06, 2014, 09:49:56 PM »
Maybe copy from addr.data instead of addr

Sent from my SCH-I535 using Tapatalk

Do not use this post as information for making any important decisions. The only agreements I ever make are informal and non-binding. Take the same precautions as when dealing with a compromised account, scammer, sockpuppet, etc.

Offline toast

Re: Handling keys (API)
« Reply #10 on: May 06, 2014, 09:50:43 PM »
 also sizeo(addr.data)

Sent from my SCH-I535 using Tapatalk

Do not use this post as information for making any important decisions. The only agreements I ever make are informal and non-binding. Take the same precautions as when dealing with a compromised account, scammer, sockpuppet, etc.

Offline jamesc

Re: Handling keys (API)
« Reply #11 on: May 08, 2014, 04:34:37 PM »
I figured it out.. bin_addr is 4 bytes longer than addr...

 

Google+