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:
Ryan O'Hara-Reid
2022-03-21 13:58:08 +11:00
committed by GitHub
parent 58b9f8b9ec
commit 09fa2f236a
122 changed files with 3006 additions and 2126 deletions

View File

@@ -765,8 +765,14 @@ func (b *Binance) SendAPIKeyHTTPRequest(ctx context.Context, ePath exchange.URL,
if err != nil {
return err
}
creds, err := b.GetCredentials(ctx)
if err != nil {
return err
}
headers := make(map[string]string)
headers["X-MBX-APIKEY"] = b.API.Credentials.Key
headers["X-MBX-APIKEY"] = creds.Key
item := &request.Item{
Method: http.MethodGet,
Path: endpointPath + path,
@@ -783,9 +789,11 @@ func (b *Binance) SendAPIKeyHTTPRequest(ctx context.Context, ePath exchange.URL,
// SendAuthHTTPRequest sends an authenticated HTTP request
func (b *Binance) SendAuthHTTPRequest(ctx context.Context, ePath exchange.URL, method, path string, params url.Values, f request.EndpointLimit, result interface{}) error {
if !b.AllowAuthenticatedRequest() {
return fmt.Errorf("%s %w", b.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
creds, err := b.GetCredentials(ctx)
if err != nil {
return err
}
endpointPath, err := b.API.Endpoints.GetURL(ePath)
if err != nil {
return err
@@ -807,13 +815,13 @@ func (b *Binance) SendAuthHTTPRequest(ctx context.Context, ePath exchange.URL, m
var hmacSigned []byte
hmacSigned, err = crypto.GetHMAC(crypto.HashSHA256,
[]byte(signature),
[]byte(b.API.Credentials.Secret))
[]byte(creds.Secret))
if err != nil {
return nil, err
}
hmacSignedStr := crypto.HexEncodeToString(hmacSigned)
headers := make(map[string]string)
headers["X-MBX-APIKEY"] = b.API.Credentials.Key
headers["X-MBX-APIKEY"] = creds.Key
fullPath = common.EncodeURLValues(fullPath, params)
fullPath += "&signature=" + hmacSignedStr
return &request.Item{
@@ -1098,9 +1106,14 @@ func (b *Binance) GetWsAuthStreamKey(ctx context.Context) (string, error) {
return "", err
}
creds, err := b.GetCredentials(ctx)
if err != nil {
return "", err
}
var resp UserAccountStream
headers := make(map[string]string)
headers["X-MBX-APIKEY"] = b.API.Credentials.Key
headers["X-MBX-APIKEY"] = creds.Key
item := &request.Item{
Method: http.MethodPost,
Path: endpointPath + userAccountStream,
@@ -1131,12 +1144,18 @@ func (b *Binance) MaintainWsAuthStreamKey(ctx context.Context) error {
listenKey, err = b.GetWsAuthStreamKey(ctx)
return err
}
creds, err := b.GetCredentials(ctx)
if err != nil {
return err
}
path := endpointPath + userAccountStream
params := url.Values{}
params.Set("listenKey", listenKey)
path = common.EncodeURLValues(path, params)
headers := make(map[string]string)
headers["X-MBX-APIKEY"] = b.API.Credentials.Key
headers["X-MBX-APIKEY"] = creds.Key
item := &request.Item{
Method: http.MethodPut,
Path: path,

View File

@@ -36,7 +36,7 @@ var (
)
func areTestAPIKeysSet() bool {
return b.ValidateAPICredentials()
return b.ValidateAPICredentials(b.GetDefaultCredentials()) == nil
}
func setFeeBuilder() *exchange.FeeBuilder {

View File

@@ -1316,7 +1316,7 @@ func (b *Binance) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuil
if feeBuilder == nil {
return 0, fmt.Errorf("%T %w", feeBuilder, common.ErrNilPointer)
}
if (!b.AllowAuthenticatedRequest() || b.SkipAuthCheck) && // Todo check connection status
if (!b.AreCredentialsValid(ctx) || b.SkipAuthCheck) && // Todo check connection status
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
feeBuilder.FeeType = exchange.OfflineTradeFee
}