0

I'm trying to build a bitcoin miner. I have generated the message successfully, but I can't get the double SHA256 correctly. The value I'm getting is incorrect. What is wrong with this code?

 Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

    Dim message_combined as string = "02000000671D0E2FF45DD1E927A51219D1CA1065C93B0C4E8840290A00000000000000002CD900FC3513260DF5BD2EABFD456CD2B3D2BACE30CC078215A907C045F4992E3E5F4A5E747B1B1843F740C0"


    Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(message_combined)
    Dim hash As Byte() = sha256.ComputeHash(bytes)
    Dim stringBuilder1 As New StringBuilder()

    For i As Integer = 0 To hash.Length - 1
        stringBuilder1.Append(hash(i).ToString("X2"))
    Next

    '  Dim sha256 As SHA256 = SHA256Managed.Create()
    Dim sha256_2 As SHA256 = SHA256Managed.Create()
    Dim bytes2 As Byte() = Encoding.UTF8.GetBytes(stringBuilder1.ToString())
    Dim hash2 As Byte() = sha256_2.ComputeHash(bytes2)
    'bytes2 = Encoding.UTF8.GetBytes(bytes)
    'hash2 = sha256_2.ComputeHash(bytes2)
    Dim stringBuilder2 As New StringBuilder()

    For i As Integer = 0 To hash2.Length - 1
        stringBuilder2.Append(hash2(i).ToString("X2"))
    Next


    final_hash = stringBuilder2.ToString()

    TextBox9.Text = final_hash
End Sub

After double hashing, the end hash should be: 920F0D3EF835E723DBDA8AE0E1EA0705FAF53AC784A8116639BCB215F3BC9D8B

| improve this question | | | | |
1

You seem to be interpreting the hex encoded data as a UTF-8 string. Instead, you should be decoding it as a byte dump.

To illustrate, decoding 12 as a UTF-8 string will give you:

0x3132

while decoding it as a hex dump will give you:

0x12

In bitcoin, all hashing it done on the byte values, which are often printed as hex strings for convenience - you must decode them back into a byte array as hex strings, not as actual strings.

| improve this answer | | | | |
  • Thank you, it solved the problem. :) – Pretty Girl Feb 17 at 14:53
  • I have another dumb question, suppose now I got the hash lower than the target, then what should I do? how to insert it to the block chain that it will generate the reward to my wallet. no need of code, just some information would be great. should I hash again with my wallet address etc and send it to some ip adress etc (web request etc)? I really have no idea. – Pretty Girl Feb 17 at 14:58
  • You're supposed to be hashing the result of getblocktemplate, which will include a coinbase transaction that pays the reward to your wallet. You cannot choose to pay the reward to yourself after the hashing is already done. If you find a valid block,, you can use the submitblock RPC call to submit it to the network – Raghav Sood Feb 17 at 15:12
  • getblocktemplate is to connect to the mining pools right? I will not use mining pools, I will mine as a sole miner (I know it's nearly impossible to find a block) I want to submit the block myself. currently I hash with version, previous block hash, merkle root, time stamp, bits, nonce. , where do I hash my wallet etc to receive the reward? – Pretty Girl Feb 17 at 15:25
  • The block template can be used to mine to your own wallet as well. Your own wallet goes in the coinbase transaction, which is part of the merkle tree, which leads to the merkle root in your block header. – Raghav Sood Feb 17 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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