sha256bitcoin

Calculate hash from getwork function - how to do it?


when I call getwork on my bitcoind server, I get the following:

./bitcoind getwork
{
    "midstate" : "695d56ae173bbd0fd5f51d8f7753438b940b7cdd61eb62039036acd1af5e51e3",
    "data" : "000000013d9dcbbc2d120137c5b1cb1da96bd45b249fd1014ae2c2b400001511000000009726fba001940ebb5c04adc4450bdc0c20b50db44951d9ca22fc5e75d51d501f4deec2711a1d932f00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000",
    "hash1" : "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000",
    "target" : "00000000000000000000000000000000000000000000002f931d000000000000"
}

This protocol does not seem to be documented. How do I compute the hash from this data. I think that this data is in little endian. So the first step is to convert everything to big endian? Once that is done, I calculate the sha256 of the data. The data can be divided in two chuncks of 64 bytes each. The hash of the first chuck is given by midstate and therefore does not have to be computed.

I must therefore hash the chunck #2 with sha256, using the midstate as the initial hash values. Once that is done, I end up with a hash of chunk 2, which is 32 bytes. I calculate the hash of this chunk one more time to get a final hash.

Then, do I convert everything to little endian and submit the work?

What is hash1 used for?


Solution

  • It sounds right, there is a script in javascript that do calculate the hash but I do not fully understand it so I don't know, maybe you understand it better if you look.

    this.tryHash = function(midstate, half, data, hash1, target, nonce){  
        data[3] = nonce;
        this.sha.reset();
    
        var h0 = this.sha.update(midstate, data).state;   // compute first hash
        for (var i = 0; i < 8; i++) hash1[i] = h0[i];   // place it in the h1 holder
        this.sha.reset();                 // reset to initial state
        var h = this.sha.update(hash1).state;       // compute final hash
        if (h[7] == 0) {
          var ret = [];
          for (var i = 0; i < half.length; i++)
            ret.push(half[i]);
          for (var i = 0; i < data.length; i++)
            ret.push(data[i]);
          return ret;
        } else return null;
      };
    

    SOURCE: https://github.com/jwhitehorn/jsMiner/blob/4fcdd9042a69b309035dfe9c9ddf716119831a16/engine.js#L149-165