forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signing.go
41 lines (34 loc) · 883 Bytes
/
signing.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
package coinbasepro
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"strconv"
"time"
)
func 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
}
func (m Message) Sign(secret, key, passphrase string) (SignedMessage, error) {
method := "GET"
url := "/users/self/verify"
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
sig, err := generateSig(fmt.Sprintf("%s%s%s", timestamp, method, url), secret)
return SignedMessage{
Message: m,
Key: key,
Passphrase: passphrase,
Timestamp: strconv.FormatInt(time.Now().Unix(), 10),
Signature: sig,
}, err
}