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

@@ -78,20 +78,28 @@ func (i *ItBit) GetTradeHistory(ctx context.Context, currencyPair, tradeID strin
// page - [optional] page to return example 1. default 1
// perPage - [optional] items per page example 50, default 50 max 50
func (i *ItBit) GetWallets(ctx context.Context, params url.Values) ([]Wallet, error) {
creds, err := i.GetCredentials(ctx)
if err != nil {
return nil, err
}
var resp []Wallet
params.Set("userId", i.API.Credentials.ClientID)
params.Set("userId", creds.ClientID)
path := fmt.Sprintf("/%s?%s", itbitWallets, params.Encode())
return resp, i.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, path, nil, &resp)
}
// CreateWallet creates a new wallet with a specified name.
func (i *ItBit) CreateWallet(ctx context.Context, walletName string) (Wallet, error) {
creds, err := i.GetCredentials(ctx)
if err != nil {
return Wallet{}, err
}
resp := Wallet{}
params := make(map[string]interface{})
params["userId"] = i.API.Credentials.ClientID
params["userId"] = creds.ClientID
params["name"] = walletName
err := i.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, "/"+itbitWallets, params, &resp)
err = i.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, "/"+itbitWallets, params, &resp)
if err != nil {
return resp, err
}
@@ -299,8 +307,9 @@ func (i *ItBit) SendHTTPRequest(ctx context.Context, ep exchange.URL, path strin
// SendAuthenticatedHTTPRequest sends an authenticated request to itBit
func (i *ItBit) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL, method, path string, params map[string]interface{}, result interface{}) error {
if !i.AllowAuthenticatedRequest() {
return fmt.Errorf("%s %w", i.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
creds, err := i.GetCredentials(ctx)
if err != nil {
return err
}
endpoint, err := i.API.Endpoints.GetURL(ep)
if err != nil {
@@ -340,14 +349,14 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.UR
var hmac []byte
hmac, err = crypto.GetHMAC(crypto.HashSHA512,
[]byte(urlPath+string(hash)),
[]byte(i.API.Credentials.Secret))
[]byte(creds.Secret))
if err != nil {
return nil, err
}
signature := crypto.Base64Encode(hmac)
headers := make(map[string]string)
headers["Authorization"] = i.API.Credentials.ClientID + ":" + signature
headers["Authorization"] = creds.ClientID + ":" + signature
headers["X-Auth-Timestamp"] = timestamp
headers["X-Auth-Nonce"] = n
headers["Content-Type"] = "application/json"

View File

@@ -311,7 +311,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 i.ValidateAPICredentials()
return i.ValidateAPICredentials(i.GetDefaultCredentials()) == nil
}
func TestSubmitOrder(t *testing.T) {

View File

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