Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
broderickhyman committed Jun 18, 2021
1 parent 0dcd361 commit 01ef216
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions client/uploader_http_pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package client

import (
"bytes"
"net/http"
"net/url"
"strings"
"encoding/json"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/broderickhyman/albiondata-client/log"
)
Expand All @@ -20,14 +20,14 @@ type httpUploaderPow struct {
}

type Pow struct {
Key string `json:"key"`
Wanted string `json:"wanted"`
Key string `json:"key"`
Wanted string `json:"wanted"`
}

// newHTTPUploaderPow creates a new HTTP uploader
func newHTTPUploaderPow(url string) uploader {
return &httpUploaderPow{
baseURL: strings.Replace(url, "http+pow", "http", -1),
baseURL: strings.Replace(url, "http+pow", "http", -1),
transport: &http.Transport{},
}
}
Expand Down Expand Up @@ -63,9 +63,9 @@ func (u *httpUploaderPow) uploadWithPow(pow Pow, solution string, natsmsg []byte
fullURL := u.baseURL + "/pow/" + topic

resp, err := http.PostForm(fullURL, url.Values{
"key": {pow.Key},
"key": {pow.Key},
"solution": {solution},
"natsmsg": {string(natsmsg)},
"natsmsg": {string(natsmsg)},
})

if err != nil {
Expand All @@ -83,20 +83,20 @@ func (u *httpUploaderPow) uploadWithPow(pow Pow, solution string, natsmsg []byte

// Generates a random hex string e.g.: faa2743d9181dca5
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}

// Converts a string to bits e.g.: 0110011...
func toBinaryBytes(s string) string {
var buffer bytes.Buffer
for i := 0; i < len(s); i++ {
fmt.Fprintf(&buffer, "%08b", s[i])
}
return fmt.Sprintf("%s", buffer.Bytes())
var buffer bytes.Buffer
for i := 0; i < len(s); i++ {
fmt.Fprintf(&buffer, "%08b", s[i])
}
return fmt.Sprintf("%s", buffer.Bytes())
}

// Solves a pow looping through possible solutions
Expand All @@ -106,7 +106,7 @@ func solvePow(pow Pow) string {
solution := ""
for {
randhex, _ := randomHex(8)
if strings.HasPrefix(toBinaryBytes(fmt.Sprintf("%x", sha256.Sum256([]byte("aod^" + randhex + "^" + pow.Key)))), pow.Wanted) {
if strings.HasPrefix(toBinaryBytes(fmt.Sprintf("%x", sha256.Sum256([]byte("aod^"+randhex+"^"+pow.Key)))), pow.Wanted) {
log.Debugf("SOLVED!")
solution = randhex
break
Expand Down

0 comments on commit 01ef216

Please sign in to comment.