What am I doing wrong?
I've implemented the GO code but I'm unable to find a header hash that divides the origin.
This is the first time I have used GO, so perhaps you can spot the problem with my code?
Can someone please give me the header hash for block 383313?
My code looks like this:
package main
import
(
"fmt"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"math/big"
)
func main() {
//Block:383313
//Version:2
//PrevBlockHash:9c4fc11576808cf7c139f0aff73de45e924d15f872d8c3cb541c88e3e1b0d7aa
//MerkleRoot:b15059d72023db53072feb6f1d2aec768971e9462f060830fcce0fb9c7372d35
//Time:January 31, 2014, 08:21:42
//Bits:0a6657d6
//Nonce:33557011
//Origin:511894124314277474466966190582380266982082554847978849190464099863530478638974696162270693661640
origin, err0 := base64.StdEncoding.DecodeString("511894124314277474466966190582380266982082554847978849190464099863530478638974696162270693661640")
prevBlock, err1 := base64.StdEncoding.DecodeString("9c4fc11576808cf7c139f0aff73de45e924d15f872d8c3cb541c88e3e1b0d7aa")
merkleRoot, err2 := base64.StdEncoding.DecodeString("b15059d72023db53072feb6f1d2aec768971e9462f060830fcce0fb9c7372d35")
bh := BlockHeader{2, prevBlock, merkleRoot, 1391156502, 174479318, 33557011}
fmt.Println("Header hash byte[]:")
fmt.Println(HeaderHash(bh))
fmt.Println("Decode errors:")
fmt.Println(err0)
fmt.Println(err1)
fmt.Println(err2)
s := HeaderHash(bh)
//fmt.Println(s)
//fmt.Println(new(big.Int).SetBytes(s))
//Reverse the hash
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
headerInt := new(big.Int).SetBytes(s)
originInt := new(big.Int).SetBytes(origin)
fmt.Println("Header hash reversed:")
fmt.Println(s)
fmt.Println("Header hash as Integer:")
fmt.Println(headerInt)
m:=big.NewInt(0)
t:=big.NewInt(0)
m.Mod(originInt, headerInt)
if m == t {
fmt.Println(">>>Origin is divisable by header hash.<<<")
} else {
fmt.Println(">>>Origin is NOT divisable by header hash.<<<")}
}
type BlockHeader struct {
Version uint32
HashPrevBlock []byte
HashMerkleRoot []byte
Timestamp uint32
Bits uint32
Nonce uint32
}
func HeaderHash(bh BlockHeader) []byte {
hash := sha256.New()
binary.Write(hash, binary.LittleEndian, bh.Version)
hash.Write(bh.HashPrevBlock)
hash.Write(bh.HashMerkleRoot)
binary.Write(hash, binary.LittleEndian, bh.Timestamp)
binary.Write(hash, binary.LittleEndian, bh.Bits)
binary.Write(hash, binary.LittleEndian, bh.Nonce)
data := sha256.Sum256(hash.Sum(nil))
return data[:]
}
Output:
Header hash byte[]:
[34 140 82 79 175 251 189 163 152 248 127 16 244 18 158 157 152 174 143 152 249 172 44 241 81 121 251 85 246 224 7 7]
Decode errors:
Header hash reversed:
[7 7 224 246 85 251 121 81 241 44 172 249 152 143 174 152 157 158 18 244 16 127 248 152 163 189 251 175 79 82 140 34]
Header hash as Integer:
3180110501919869770927571415789333083034902861520772082565710950573131598882
>>>Origin is NOT divisable by header hash.<<<
Program exited.