Author Topic: Handling keys (API)  (Read 2884 times)

0 Members and 1 Guest are viewing this topic.

Offline jamesc

I figured it out.. bin_addr is 4 bytes longer than addr...

Offline toast

  • Hero Member
  • *****
  • Posts: 4001
    • View Profile
  • BitShares: nikolai
 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 toast

  • Hero Member
  • *****
  • Posts: 4001
    • View Profile
  • BitShares: nikolai
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 jamesc

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 bytemaster



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

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

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 bytemaster

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

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 jamesc

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

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

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 »