0

I have same problem whit create a double sha256 of the transactions and blocks with C++.

The hash block it is created with the concatenation of these data,

version + previusBlock + merkleRoot + timeStamp + bit + nonce

In the hash block is not necessary to include the all data raw transaction because is are include in the merkleRoot, right?

For create, hash transaction is create whit the concatenation of these data, versionRawTransaction + output of the transaction input + scriptSing + sequences transaction input + cAmmount transaction output + publicKeyScript + lockTime raw transaction

Or are include the data for saving the variable structure? in this method

numberRawTransactions + versionRawTransaction + numbarTransactionInput + output of the transaction input + scriptLenght + scriptSing + sequences transaction input + numbarTransactionOutput + cAmmount transaction output + publicKeyScriptLenght + publicKeyScript + lockTime raw transaction

Now I've tried to apply this theory in my C ++ program but can't get the desired hash. Can you help me?

This is the my test code

    TEST(hash_test, first_test_double_sha_bit_genesi_block_bitcoin_crypolibrary)
{
    //Init logger
    FLAGS_minloglevel = 0;
    FLAGS_logtostderr = true;
    google::SetLogDestination(google::GLOG_WARNING,  "/home/vincenzo/Github/SpyCblock/test/log/first_test_sha_not_on_bitcoin_protocolo.log");

    string version = "01000000";
    string previusBlock = "0000000000000000000000000000000000000000000000000000000000000000";
    string merkleRoot = "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a";
    string timeStamp = "29ab5f49";
    string bit = "ffff001d";
    string nonce = "1dac2b7c";

    stringstream stream;

    stream  << version << previusBlock << merkleRoot << timeStamp << bit << nonce;


    Bytes byte = asciiBytes(stream.str().c_str());
    Sha256Hash shaHash = Sha256::getDoubleHash(byte.data(), byte.size());

    LOG(INFO) << "The hash genesi block converting with double sha256: " << shaHash.ToString();
    ASSERT_EQ(shaHash.ToString(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");


}

but I get this result 46f4d3f53251d05ab58a3f8925ce7023f2115dc38468ec78521c7d0e1dc7bd19

Since I know practically nothing in cryptography in practice, I relied on this library where I use this type of data here that makes the double sha256 Find here

Sorry for my terrible English but I'm learning

1 Answer 1

1

The problem is that you are hashing the string of the hexadecimal representation of the bytes of the block header, not the bytes of the block header itself. You need to hash the bytes of the block header itself.

You need to set everything as a byte array, not a string. So you would have something like this for the entire block header all together:

Byte[] block_header = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd,
                       0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76,
                       0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
                       0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab,
                       0x5f, 0x49, 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c};

It's possible convert the string hex into matrix byte in C++ with this code

  vector<unsigned char> ToHexIntoVectorByte(string &hexData)
{

  vector<unsigned char> *bytes = new vector<unsigned char>();
  for(unsigned i = 0; i < hexData.length(); i += 2)
  {
      string byteSubString = hexData.substr(i, 2);
      unsigned char byte = (unsigned char)strtol(byteSubString.c_str(), NULL, 16);
      bytes->push_back(byte);

  }
  return *bytes;
}

for extract the matrix in the unsigned char using vectorresult.data() and if using the Bitcoin-Cryptography-Library the final code is

vector<unsigned char> vectorByte = ToHexIntoVectorByte(stringHex);

Sha256Hash shaHash = Sha256::getDoubleHash(vectorByte.data(), vectorByte.size());
1
  • Do you recommend some C ++ functions that convert the string into bytes? Mar 25, 2019 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.