forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
130 lines (106 loc) · 2.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package coinbase
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"time"
)
type Client struct {
BaseURL string
Secret string
Key string
Passphrase string
}
func NewClient(secret, key, passphrase string) *Client {
client := Client{
BaseURL: "https://api.gdax.com",
Secret: secret,
Key: key,
Passphrase: passphrase,
}
return &client
}
func (c *Client) Request(method string, url string,
params, result interface{}) (res *http.Response, err error) {
var data []byte
body := bytes.NewReader(make([]byte, 0))
if params != nil {
data, err = json.Marshal(params)
if err != nil {
return res, err
}
body = bytes.NewReader(data)
}
fullURL := fmt.Sprintf("%s%s", c.BaseURL, url)
fmt.Printf("fullURL: %v\n", fullURL)
req, err := http.NewRequest(method, fullURL, body)
if err != nil {
return res, err
}
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
// XXX: Sandbox time is off right now
if os.Getenv("TEST_COINBASE_OFFSET") != "" {
inc, err := strconv.Atoi(os.Getenv("TEST_COINBASE_OFFSET"))
if err != nil {
return res, err
}
timestamp = strconv.FormatInt(time.Now().Unix()+int64(inc), 10)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "Baylatent Bot 2.0")
req.Header.Add("CB-ACCESS-KEY", c.Key)
req.Header.Add("CB-ACCESS-PASSPHRASE", c.Passphrase)
req.Header.Add("CB-ACCESS-TIMESTAMP", timestamp)
message := fmt.Sprintf("%s%s%s%s", timestamp, method, url,
string(data))
sig, err := c.generateSig(message, c.Secret)
if err != nil {
return res, err
}
req.Header.Add("CB-ACCESS-SIGN", sig)
client := http.Client{}
res, err = client.Do(req)
if err != nil {
return res, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
s := buf.String()
println ("body:" + s)
defer res.Body.Close()
coinbaseError := Error{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&coinbaseError); err != nil {
return res, err
}
return res, error(coinbaseError)
}
if result != nil {
decoder := json.NewDecoder(res.Body)
if err = decoder.Decode(result); err != nil {
return res, err
}
}
return res, nil
}
func (c *Client) generateSig(message, secret string) (string, error) {
key, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return "", err
}
signature := hmac.New(sha256.New, key)
_, err = signature.Write([]byte(message))
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(signature.Sum(nil)), nil
}