Created
January 1, 2014 23:23
-
-
Save artursapek/8212775 to your computer and use it in GitHub Desktop.
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 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