-
Notifications
You must be signed in to change notification settings - Fork 0
/
private-queries.go
73 lines (61 loc) · 1.54 KB
/
private-queries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"time"
"crypto/hmac"
"crypto/sha512"
"crypto/sha256"
"strconv"
"net/http"
"encoding/base64"
"fmt"
"io/ioutil"
)
var client = &http.Client{}
type PostData struct {
Nonce int64 `json:"nonce"`
}
func Nonce() int64 {
return time.Now().UnixNano()
}
func postAccountBalance() {
url := "https://api.kraken.com/0/private/Balance"
uri := "/0/private/Balance"
req, err := http.NewRequest("POST", url, nil)
if err != nil {
panic(err)
}
req.Header.Add("API-Key", APIKEY)
signature := signAPI(uri, "")
req.Header.Add("API-Sign", signature)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
fmt.Println(resp.Body)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
func signAPI(uri string, postData string) string {
nonce := Nonce()
postData = "nonce=" + strconv.FormatInt(nonce, 10) + "&" + postData
// Calculate the SHA256 of the nonce and the POST data
sha := sha256.New()
sha.Write([]byte(postData))
message := sha.Sum(nil)
// Decode the API secret (the private part of the API key) from base64
decoded, err := base64.StdEncoding.DecodeString(PRIVATEKEY)
if err != nil {
panic(err)
}
// Calculate the HMAC of the URI path and the SHA256, using SHA512 as the
// HMAC hash and the decoded API secret as the HMAC key
mac := hmac.New(sha512.New, decoded)
mac.Write(message)
sum := mac.Sum(nil)
// Encode the HMAC into base64
return base64.StdEncoding.EncodeToString(sum)
}