Skip to content

Instantly share code, notes, and snippets.

@worace
Created January 22, 2015 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save worace/b742361a76c194de08df to your computer and use it in GitHub Desktop.
Save worace/b742361a76c194de08df to your computer and use it in GitHub Desktop.
crude golang gitcoin miner for https://github.com/worace/git-coin
package main
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
)
const baseUrl string = "http://git-coin.herokuapp.com"
func gcUrl(path string) string {
return fmt.Sprintf("%s%s", baseUrl, path)
}
func newMessage() string {
return fmt.Sprintf("%d", rand.Int())
}
func submit(message string) {
}
func digest(input string) []byte {
h := sha1.New()
h.Write([]byte(input))
return h.Sum(nil)
}
func fetchTarget() []byte {
resp, err := http.Get(gcUrl("/target"))
if err != nil {
panic("Couldn't fetch target")
}
target, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic("error reading response body")
}
defer resp.Body.Close()
return target
}
func submitMessage(message string) (resp *http.Response, err error) {
return http.PostForm(gcUrl("/hash"), url.Values{"owner": {"worace"}, "message": {message}})
}
func main() {
currentTarget := fetchTarget()
for {
input := newMessage()
output := digest(input)
targetBytes, _ := hex.DecodeString(string(currentTarget))
//targetString := fmt.Sprintf("%x", currentTarget)
//outputString := fmt.Sprintf("%x", hex.EncodeToString(output))
//fmt.Println(targetBytes)
//fmt.Println(output)
if bytes.Compare(output, targetBytes) < 0 {
fmt.Println("congrats got a hash!")
resp, err := submitMessage(input)
if err != nil {
panic("failed to submit guess")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic("error reading response body")
}
fmt.Println(string(body))
currentTarget = fetchTarget()
} else {
fmt.Println("target still smaller, keep searching")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment