-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
58 lines (51 loc) · 1.43 KB
/
client.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
package btce
import (
"bytes"
"strings"
"net/url"
"net/http"
"crypto/tls"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
)
const (
PRIVATE_API_ENDPOINT = "https://btc-e.com/tapi"
)
type clientTLS struct {
public string
secret string
hmac bool
}
func NewClient(public string, secret string, hmac bool) *clientTLS {
return &clientTLS{public, secret, hmac};
}
func (this *clientTLS) HMAC_SHA512(secret string, message string) string {
hash := hmac.New(sha512.New, []byte(secret));
hash.Write([]byte(message));
return strings.ToLower(hex.EncodeToString(hash.Sum(nil)));
}
func (this *clientTLS) Request(data url.Values, location string) (*http.Response, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true },
};
httpClient := &http.Client{Transport: tr};
params := data.Encode();
if this.hmac == true {
request, err := http.NewRequest("POST", PRIVATE_API_ENDPOINT, bytes.NewBufferString(params));
if err != nil {
return nil, err;
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded");
request.Header.Set("Key", this.public);
request.Header.Set("Sign", this.HMAC_SHA512(this.secret, params));
return httpClient.Do(request);
} else {
request, err := http.NewRequest("GET", location, bytes.NewBufferString(params));
if err != nil {
return nil, err;
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded");
return httpClient.Do(request);
}
}