mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-08 07:26:48 +00:00
context: Add authenticated HTTP credentials (#892)
* gRPC: context overide * exchanges: continue update * exchange: Update context handling *Add setter methods for API credentials *Shift credentials functionality to its own file in exchanges package *Add tests *Refactor function DeployCredentialsToContext for library usage *Add function to process credential metadata from API boundary to internal use context value. *Add OTP rpc handling * exchanges: reverts to old style in GetFeeByType, reverts some code I accidently deleted. Plus things and other. XD * template: update * exchanges: fix linter issues * REMOVE THAT AWESOME NEW LINE! * gct: fix some tests * I cant spell :( * exchanges/gctscript: fix more tests * coinnut: fix tests * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * glorious: nits * exchanges/gctcli: stop applying empty credentials * fix linters * exchanges: add test * rpceserver: actually check error for errors * rpcserver: fix up tests * Update exchanges/credentials.go Co-authored-by: Scott <gloriousCode@users.noreply.github.com> * exchanges/creds: move tests to corresponding files, add protection and segration for Credentials struct & ptr values * exchanges/creds: allow subaccount to override default credentials via gRPC * exchanges/credentials: don't return nil in GetCredentials * creds: spelling * exchanges: fix glorious NITS! * credentials: Add in test and refactor IsEmpty method. * credentials: change type positioning (glorious) * exchange_template: Fix template changes * DOCS: Refresh * docs: fix spelling * DOCS: fix alignment and add package * DOCS: ALIGN! Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
@@ -401,16 +401,16 @@ func (g *Gateio) GetTradeHistory(ctx context.Context, symbol string) (TradHistor
|
||||
}
|
||||
|
||||
// GenerateSignature returns hash for authenticated requests
|
||||
func (g *Gateio) GenerateSignature(message string) ([]byte, error) {
|
||||
return crypto.GetHMAC(crypto.HashSHA512, []byte(message),
|
||||
[]byte(g.API.Credentials.Secret))
|
||||
func (g *Gateio) GenerateSignature(secret, message string) ([]byte, error) {
|
||||
return crypto.GetHMAC(crypto.HashSHA512, []byte(message), []byte(secret))
|
||||
}
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends authenticated requests to the Gateio API
|
||||
// To use this you must setup an APIKey and APISecret from the exchange
|
||||
func (g *Gateio) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL, method, endpoint, param string, result interface{}) error {
|
||||
if !g.AllowAuthenticatedRequest() {
|
||||
return fmt.Errorf("%s %w", g.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
|
||||
creds, err := g.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ePoint, err := g.API.Endpoints.GetURL(ep)
|
||||
if err != nil {
|
||||
@@ -418,9 +418,9 @@ func (g *Gateio) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.U
|
||||
}
|
||||
headers := make(map[string]string)
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
headers["key"] = g.API.Credentials.Key
|
||||
headers["key"] = creds.Key
|
||||
|
||||
hmac, err := g.GenerateSignature(param)
|
||||
hmac, err := g.GenerateSignature(creds.Secret, param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -332,7 +332,7 @@ func TestGetOrderHistory(t *testing.T) {
|
||||
// Any tests below this line have the ability to impact your orders on the exchange. Enable canManipulateRealOrders to run them
|
||||
// ----------------------------------------------------------------------------------------------------------------------------
|
||||
func areTestAPIKeysSet() bool {
|
||||
return g.ValidateAPICredentials()
|
||||
return g.ValidateAPICredentials(g.GetDefaultCredentials()) == nil
|
||||
}
|
||||
|
||||
func TestSubmitOrder(t *testing.T) {
|
||||
@@ -525,7 +525,7 @@ func TestWsGetBalance(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go g.wsReadData()
|
||||
err = g.wsServerSignIn()
|
||||
err = g.wsServerSignIn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -550,7 +550,7 @@ func TestWsGetOrderInfo(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go g.wsReadData()
|
||||
err = g.wsServerSignIn()
|
||||
err = g.wsServerSignIn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gateio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -44,7 +45,7 @@ func (g *Gateio) WsConnect() error {
|
||||
go g.wsReadData()
|
||||
|
||||
if g.GetAuthenticatedAPISupport(exchange.WebsocketAuthentication) {
|
||||
err = g.wsServerSignIn()
|
||||
err = g.wsServerSignIn(context.TODO())
|
||||
if err != nil {
|
||||
g.Websocket.DataHandler <- err
|
||||
g.Websocket.SetCanUseAuthenticatedEndpoints(false)
|
||||
@@ -67,9 +68,13 @@ func (g *Gateio) WsConnect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Gateio) wsServerSignIn() error {
|
||||
func (g *Gateio) wsServerSignIn(ctx context.Context) error {
|
||||
creds, err := g.GetCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nonce := int(time.Now().Unix() * 1000)
|
||||
sigTemp, err := g.GenerateSignature(strconv.Itoa(nonce))
|
||||
sigTemp, err := g.GenerateSignature(creds.Secret, strconv.Itoa(nonce))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -77,7 +82,7 @@ func (g *Gateio) wsServerSignIn() error {
|
||||
signinWsRequest := WebsocketRequest{
|
||||
ID: g.Websocket.Conn.GenerateMessageID(false),
|
||||
Method: "server.sign",
|
||||
Params: []interface{}{g.API.Credentials.Key, signature, nonce},
|
||||
Params: []interface{}{creds.Key, signature, nonce},
|
||||
}
|
||||
resp, err := g.Websocket.Conn.SendMessageReturnResponse(signinWsRequest.ID,
|
||||
signinWsRequest)
|
||||
|
||||
@@ -701,7 +701,7 @@ func (g *Gateio) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuild
|
||||
if feeBuilder == nil {
|
||||
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
|
||||
}
|
||||
if !g.AllowAuthenticatedRequest() && // Todo check connection status
|
||||
if !g.AreCredentialsValid(ctx) && // Todo check connection status
|
||||
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
|
||||
feeBuilder.FeeType = exchange.OfflineTradeFee
|
||||
}
|
||||
@@ -859,8 +859,8 @@ func (g *Gateio) GetOrderHistory(ctx context.Context, req *order.GetOrdersReques
|
||||
}
|
||||
|
||||
// AuthenticateWebsocket sends an authentication message to the websocket
|
||||
func (g *Gateio) AuthenticateWebsocket(_ context.Context) error {
|
||||
return g.wsServerSignIn()
|
||||
func (g *Gateio) AuthenticateWebsocket(ctx context.Context) error {
|
||||
return g.wsServerSignIn(ctx)
|
||||
}
|
||||
|
||||
// ValidateCredentials validates current credentials used for wrapper
|
||||
|
||||
Reference in New Issue
Block a user