forked from go-numb/go-ftx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
62 lines (49 loc) · 1.05 KB
/
auth.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
package auth
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"sync"
)
type Config struct {
mux sync.RWMutex
Key string
Secret string
// SubAccountID use Account as needed when rewrite ID
SubAccountID int
subAccounts map[int]SubAccount
}
type SubAccount struct {
UUID int
Nickname string
}
func (p *Config) UseSubAccountID(uuid int) {
p.mux.Lock()
defer p.mux.Unlock()
p.SubAccountID = uuid
}
func (p *Config) SubAccount() SubAccount {
p.mux.Lock()
defer p.mux.Unlock()
return p.subAccounts[p.SubAccountID]
}
func New(key, secret string, subaccounts ...SubAccount) *Config {
config := &Config{
Key: key,
Secret: secret,
SubAccountID: 0,
}
if 0 < len(subaccounts) {
accounts := make(map[int]SubAccount)
for i := range subaccounts {
accounts[subaccounts[i].UUID] = subaccounts[i]
}
config.subAccounts = accounts
}
return config
}
func (p *Config) Signture(body string) string {
mac := hmac.New(sha256.New, []byte(p.Secret))
mac.Write([]byte(body))
return hex.EncodeToString(mac.Sum(nil))
}