Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Step 1: Generate 32 random bytes
- rand32 := make([]byte, 32)
- _, err = rand.Read(rand32)
- if err != nil {
- // TODO Handle error
- }
- // Step 2: Encode bytes in Base64 and remove special characters
- nonce := base64.StdEncoding.EncodeToString(rand32)
- re := regexp.MustCompile("[^a-zA-Z0-9]")
- nonce = re.ReplaceAllString(nonce, "")
- // Step 3: Build the base string: {Public key of pool}-{Nonce}-{Block id}-{Difficulty}
- base := fmt.Sprintf("%s-%s-%s-%s", info.Data.PublicKey, nonce, info.Data.Block, info.Data.Difficulty)
- // Step 4: Derive argon key from base (parameters are fixed)
- argonBytes := argon2.Key([]byte(base), make([]byte, 0), 1, 524288, 1, 32)
- argon := base64.StdEncoding.EncodeToString(argonBytes)
- // Step 5: Create the initial hash
- hash := fmt.Sprintf("%s%s", base, argon)
- hashBytes := []byte(hash)
- // Step 6: Hash with SHA-512 five times
- digester := sha512.New()
- for i := 0; i < 5; i++ {
- _, err := digester.Write(hashBytes)
- if err != nil {
- log.Fatalf("Hashing with SHA-512 failed: %s", err.Error())
- }
- hashBytes = digester.Sum(nil)
- digester.Reset()
- }
- // Step 7: Convert the final hash into a hex string with 128 characters
- hashHex := fmt.Sprintf("%x", hashBytes)
- // Step 8: Converts pairs of hex characerters into decimals. The final
- // slice has 64 elements.
- m := make([]int64, 0)
- for i := 0; i < len(hashHex); i = i + 2 {
- dec, err := strconv.ParseInt(string(hashHex[i:i+2]), 16, 64)
- if err != nil {
- log.Fatalf("Converting hex into dec failed: %s", err.Error())
- }
- m = append(m, dec)
- }
- // Step 9: Create the duration by concating selected components
- durationString := fmt.Sprintf("%d%d%d%d%d%d%d%d", m[10], m[15], m[20], m[23], m[31], m[40], m[45], m[55])
- // Step 10: Parse the duration into an integer. We don't care if the
- // duration is too big for int64. To find a valid nonce we need a duration
- // smaller than the max value of int64 anyway.
- duration, _ := strconv.ParseInt(durationString, 10, 64)
- // Step 11: Finally calculate the result
- diff, err := strconv.ParseInt(info.Data.Difficulty, 10, 64)
- if err != nil {
- log.Fatalf("Parsing to integer failed: ", err.Error())
- }
- result := duration / diff
- // Step 12: Check if we found a valid result
- if result > 0 && result <= int64(info.Data.Limit) {
- log.Println("Found a nonce!")
- submit, err := adapter.SubmitNonce(argon[30:], nonce, info.Data.PublicKey, "4iokkZdd6yPeeCu14S4EQ62ZuhBaND7mY94q23itVY3nWrGrMAApQyDfc7HZWWxkebc7sLEYyQADPkEmKhCd3wdf")
- if err != nil {
- fmt.Printf("Submitting a share failed: %s\n", err.Error())
- }
- fmt.Printf("Response: %v\n", submit)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement