-
Notifications
You must be signed in to change notification settings - Fork 390
/
client.go
116 lines (92 loc) · 2.59 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package coinpayments
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"net/http"
"net/url"
"github.com/zeebo/errs"
)
// Error is error class API errors.
var Error = errs.Class("coinpayments client error")
// ErrMissingPublicKey is returned when Coinpayments client is missing public key.
var ErrMissingPublicKey = errs.Class("missing public key")
// Credentials contains public and private API keys for client.
type Credentials struct {
PublicKey string
PrivateKey string
}
// Client handles base API processing.
type Client struct {
creds Credentials
http http.Client
}
// NewClient creates new instance of client with provided credentials.
func NewClient(creds Credentials) *Client {
client := &Client{
creds: creds,
http: http.Client{
Timeout: 0,
},
}
return client
}
// Transactions returns transactions API.
func (c *Client) Transactions() Transactions {
return Transactions{client: c}
}
// ConversionRates returns ConversionRates API.
func (c *Client) ConversionRates() ConversionRates {
return ConversionRates{client: c}
}
// do handles base API request routines.
func (c *Client) do(ctx context.Context, cmd string, values url.Values) (_ json.RawMessage, err error) {
if c.creds.PublicKey == "" {
return nil, Error.Wrap(ErrMissingPublicKey.New(""))
}
values.Set("version", "1")
values.Set("format", "json")
values.Set("key", c.creds.PublicKey)
values.Set("cmd", cmd)
encoded := values.Encode()
buff := bytes.NewBufferString(encoded)
req, err := http.NewRequest(http.MethodPost, "https://www.coinpayments.net/api.php", buff)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("HMAC", c.hmac([]byte(encoded)))
resp, err := c.http.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
return nil, errs.New("internal server error")
}
var data struct {
Error string `json:"error"`
Result json.RawMessage `json:"result"`
}
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
if data.Error != "ok" {
return nil, errs.New(data.Error)
}
return data.Result, nil
}
// hmac returns string representation of HMAC signature
// signed with clients private key.
func (c *Client) hmac(payload []byte) string {
mac := hmac.New(sha512.New, []byte(c.creds.PrivateKey))
_, _ = mac.Write(payload)
return hex.EncodeToString(mac.Sum(nil))
}