Skip to content

Instantly share code, notes, and snippets.

@artursapek
Created January 1, 2014 23:23
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 artursapek/8212775 to your computer and use it in GitHub Desktop.
Save artursapek/8212775 to your computer and use it in GitHub Desktop.
package btce
import (
"bytes"
"net/http"
"net/url"
"time"
"crypto/sha512"
"crypto/hmac"
"strconv"
"encoding/hex"
"io/ioutil"
)
const API_KEY string = "..."
const API_SECRET string = "..."
func nonce() string {
return strconv.FormatInt(int64(time.Now().Unix()), 10)
}
func hash(params string) string {
hash := sha512.New
h := hmac.New(hash, []byte(API_SECRET))
h.Write([]byte(params))
return hex.EncodeToString(h.Sum(nil))
}
func ApiRequest(action string, params url.Values) []byte {
client := &http.Client{}
params.Set("method", action)
params.Set("nonce", nonce())
paramsEncoded := params.Encode()
body := bytes.NewBufferString(paramsEncoded)
req, _ := http.NewRequest("POST", "https://btc-e.com/tapi", body)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
req.Header.Set("Key", API_KEY)
req.Header.Set("Sign", hash(paramsEncoded))
response, err := client.Do(req)
if err != nil {
panic(err)
} else {
body, _ := ioutil.ReadAll(response.Body)
return body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment