Created
January 22, 2015 02:42
-
-
Save worace/b742361a76c194de08df to your computer and use it in GitHub Desktop.
crude golang gitcoin miner for https://github.com/worace/git-coin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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