Minor general cleanup and bug fixes (#443)

* Bugfix, remove non-needed code and cleanup some code

* Run go mod tidy

* Remove non-needed test and fix tautological err

* Fix Huobi interim var reference
This commit is contained in:
Adrian Gallagher
2020-02-12 16:52:05 +11:00
committed by GitHub
parent 1f3bb58a6b
commit 168c966441
14 changed files with 23 additions and 256 deletions

View File

@@ -1,150 +0,0 @@
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
"github.com/thrasher-corp/gocryptotrader/common/file"
)
func encodePEM(privKey *ecdsa.PrivateKey, pubKey bool) ([]byte, error) {
if !pubKey {
x509Enc, err := x509.MarshalECPrivateKey(privKey)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(
&pem.Block{
Type: "EC PRIVATE KEY",
Bytes: x509Enc,
},
), nil
}
publicKey := &privKey.PublicKey
x509EncPub, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(
&pem.Block{
Type: "EC PUBLIC KEY",
Bytes: x509EncPub,
},
), nil
}
func decodePEM(pemPrivKey []byte) (*ecdsa.PrivateKey, error) {
block, _ := pem.Decode(pemPrivKey)
if block == nil {
return nil, errors.New("priv block data is nil")
}
x509Enc := block.Bytes
return x509.ParseECPrivateKey(x509Enc)
}
func writeFile(fileName string, data []byte) error {
return file.Write(fileName, data)
}
func main() {
genKeys := false
privKeyData, err := ioutil.ReadFile("privatekey.pem")
if err != nil {
genKeys = true
}
log.Printf("Generating keys: %v.", genKeys)
var privKey, pubKey []byte
PEMEncoder := func(p *ecdsa.PrivateKey) {
privKey, err = encodePEM(p, false)
if err != nil {
log.Fatal(err)
}
pubKey, err = encodePEM(p, true)
if err != nil {
log.Fatal(err)
}
}
if genKeys {
var pKey *ecdsa.PrivateKey
pKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatal(err)
}
PEMEncoder(pKey)
err = writeFile("privatekey.pem", privKey)
if err != nil {
log.Fatal(err)
}
err = writeFile("publickey.pem", pubKey)
if err != nil {
log.Fatal(err)
}
} else {
var pubKeyData []byte
pubKeyData, err = ioutil.ReadFile("publickey.pem")
if err != nil {
log.Fatal(err)
}
log.Println("Successfully read PEM files.")
var priv *ecdsa.PrivateKey
priv, err = decodePEM(privKeyData)
if err != nil {
log.Fatal(err)
}
PEMEncoder(priv)
if !bytes.Equal(privKey, privKeyData) {
log.Fatalf("PEM privkey mismatch")
}
if !bytes.Equal(pubKey, pubKeyData) {
log.Fatalf("PEM pubkey mismatch")
}
log.Println("PEM data matches.")
}
fmt.Println()
fmt.Println(string(privKey))
fmt.Println(string(pubKey))
type JSONGeneration struct {
PEMKey string
PEMKeySupport bool
}
r := JSONGeneration{
PEMKey: string(privKey),
PEMKeySupport: true,
}
resultk, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatal(err)
}
log.Println("Please visit https://github.com/huobiapi/API_Docs_en/wiki/Signing_API_Requests and follow from step 2 onwards.")
log.Printf("After completing the above instructions, please copy and paste the below key in the API section (including the following ',') into your Huobi exchange config file:\n\n")
fmt.Println(string(resultk[1:len(resultk)-1]) + ",")
}

View File

@@ -63,7 +63,7 @@ func getOnlineOfflinePortfolio(coins []portfolio.Coin, online bool) {
func main() {
var inFile, key string
flag.StringVar(&inFile, "infile", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&inFile, "config", config.DefaultFilePath(), "The config input file to process.")
flag.StringVar(&key, "key", "", "The key to use for AES encryption.")
flag.Parse()
@@ -132,7 +132,8 @@ func main() {
bf := bitfinex.Bitfinex{}
bf.SetDefaults()
bf.Verbose = false
ticker, errf := bf.GetTicker(y.Coin.String() + currency.USD.String())
pair := "t" + y.Coin.String() + currency.USD.String()
ticker, errf := bf.GetTicker(pair)
if errf != nil {
log.Println(errf)
} else {

View File

@@ -20,7 +20,6 @@ import (
"github.com/thrasher-corp/gocryptotrader/connchecker"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
@@ -703,8 +702,8 @@ func (c *Config) GetExchangeConfig(name string) (*ExchangeConfig, error) {
return nil, fmt.Errorf(ErrExchangeNotFound, name)
}
// GetForexProviderConfig returns a forex provider configuration by its name
func (c *Config) GetForexProviderConfig(name string) (base.Settings, error) {
// GetForexProvider returns a forex provider configuration by its name
func (c *Config) GetForexProvider(name string) (currency.FXSettings, error) {
m.Lock()
defer m.Unlock()
for i := range c.Currency.ForexProviders {
@@ -712,11 +711,11 @@ func (c *Config) GetForexProviderConfig(name string) (base.Settings, error) {
return c.Currency.ForexProviders[i], nil
}
}
return base.Settings{}, errors.New("provider not found")
return currency.FXSettings{}, errors.New("provider not found")
}
// GetForexProvidersConfig returns a list of available forex providers
func (c *Config) GetForexProvidersConfig() []base.Settings {
// GetForexProviders returns a list of available forex providers
func (c *Config) GetForexProviders() []currency.FXSettings {
m.Lock()
defer m.Unlock()
return c.Currency.ForexProviders
@@ -978,10 +977,10 @@ func (c *Config) CheckCurrencyConfigValues() error {
if len(fxProviders) != len(c.Currency.ForexProviders) {
for x := range fxProviders {
_, err := c.GetForexProviderConfig(fxProviders[x])
_, err := c.GetForexProvider(fxProviders[x])
if err != nil {
log.Warnf(log.Global, "%s forex provider not found, adding to config..\n", fxProviders[x])
c.Currency.ForexProviders = append(c.Currency.ForexProviders, base.Settings{
c.Currency.ForexProviders = append(c.Currency.ForexProviders, currency.FXSettings{
Name: fxProviders[x],
RESTPollingDelay: 600,
APIKey: DefaultUnsetAPIKey,

View File

@@ -1129,25 +1129,25 @@ func TestGetForexProviderConfig(t *testing.T) {
if err != nil {
t.Error("GetForexProviderConfig. LoadConfig error", err)
}
_, err = cfg.GetForexProviderConfig("Fixer")
_, err = cfg.GetForexProvider("Fixer")
if err != nil {
t.Error("GetForexProviderConfig error", err)
}
_, err = cfg.GetForexProviderConfig("this is not a forex provider")
_, err = cfg.GetForexProvider("this is not a forex provider")
if err == nil {
t.Error("GetForexProviderConfig no error for invalid provider")
}
}
func TestGetForexProvidersConfig(t *testing.T) {
func TestGetForexProviders(t *testing.T) {
cfg := GetConfig()
err := cfg.LoadConfig(TestFile, true)
if err != nil {
t.Error(err)
}
if r := cfg.GetForexProvidersConfig(); len(r) != 5 {
if r := cfg.GetForexProviders(); len(r) != 5 {
t.Error("unexpected length of forex providers")
}
}

View File

@@ -7,7 +7,6 @@ import (
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
"github.com/thrasher-corp/gocryptotrader/database"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
@@ -291,7 +290,7 @@ type BankTransaction struct {
// CurrencyConfig holds all the information needed for currency related manipulation
type CurrencyConfig struct {
ForexProviders []base.Settings `json:"forexProviders"`
ForexProviders []currency.FXSettings `json:"forexProviders"`
CryptocurrencyProvider CryptocurrencyProvider `json:"cryptocurrencyProvider"`
Cryptocurrencies currency.Currencies `json:"cryptocurrencies"`
CurrencyPairFormat *CurrencyPairFormatConfig `json:"currencyPairFormat"`

View File

@@ -1551,8 +1551,7 @@
},
"credentials": {
"key": "Key",
"secret": "Secret",
"pemKey": "-----BEGIN EC PRIVATE KEY-----\nJUSTADUMMY\n-----END EC PRIVATE KEY-----\n"
"secret": "Secret"
},
"credentialsValidator": {
"requiresKey": true,

View File

@@ -363,11 +363,6 @@ func (e *Engine) Start() error {
}
}
var newFxSettings []currency.FXSettings
for _, d := range e.Config.Currency.ForexProviders {
newFxSettings = append(newFxSettings, currency.FXSettings(d))
}
err := currency.RunStorageUpdater(currency.BotOverrides{
Coinmarketcap: e.Settings.EnableCoinmarketcapAnalysis,
FxCurrencyConverter: e.Settings.EnableCurrencyConverter,
@@ -376,7 +371,7 @@ func (e *Engine) Start() error {
FxOpenExchangeRates: e.Settings.EnableOpenExchangeRates,
},
&currency.MainConfiguration{
ForexProviders: newFxSettings,
ForexProviders: e.Config.GetForexProviders(),
CryptocurrencyProvider: coinmarketcap.Settings(e.Config.Currency.CryptocurrencyProvider),
Cryptocurrencies: e.Config.Currency.Cryptocurrencies,
FiatDisplayCurrency: e.Config.Currency.FiatDisplayCurrency,

View File

@@ -648,7 +648,7 @@ func (s *RPCServer) RemovePortfolioAddress(ctx context.Context, r *gctrpc.Remove
// GetForexProviders returns a list of available forex providers
func (s *RPCServer) GetForexProviders(ctx context.Context, r *gctrpc.GetForexProvidersRequest) (*gctrpc.GetForexProvidersResponse, error) {
providers := Bot.Config.GetForexProvidersConfig()
providers := Bot.Config.GetForexProviders()
if len(providers) == 0 {
return nil, fmt.Errorf("forex providers is empty")
}

View File

@@ -1158,7 +1158,7 @@ func (c *Coinbene) SendAuthHTTPRequest(method, path, epPath string, isSwap bool,
}
finalBody = bytes.NewBufferString(string(tempBody))
preSign = timestamp + method + authPath + epPath + string(tempBody)
case params == nil:
default:
preSign = timestamp + method + authPath + epPath
}
tempSign := crypto.GetHMAC(crypto.HashSHA256,

View File

@@ -2,18 +2,12 @@ package huobi
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
@@ -764,44 +758,14 @@ func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url
}
hmac := crypto.GetHMAC(crypto.HashSHA256, []byte(payload), []byte(h.API.Credentials.Secret))
signature := crypto.Base64Encode(hmac)
values.Set("Signature", signature)
if h.API.Credentials.PEMKey != "" && h.API.PEMKeySupport {
pemKey := strings.NewReader(h.API.Credentials.PEMKey)
pemBytes, err := ioutil.ReadAll(pemKey)
if err != nil {
return fmt.Errorf("%s unable to ioutil.ReadAll PEM key: %s", h.Name, err)
}
block, _ := pem.Decode(pemBytes)
if block == nil {
return fmt.Errorf("%s PEM block is nil", h.Name)
}
x509Encoded := block.Bytes
privKey, err := x509.ParseECPrivateKey(x509Encoded)
if err != nil {
return fmt.Errorf("%s unable to ParseECPrivKey: %s", h.Name, err)
}
r, s, err := ecdsa.Sign(rand.Reader, privKey, crypto.GetSHA256([]byte(signature)))
if err != nil {
return fmt.Errorf("%s unable to sign: %s", h.Name, err)
}
privSig := r.Bytes()
privSig = append(privSig, s.Bytes()...)
values.Set("PrivateSignature", crypto.Base64Encode(privSig))
}
values.Set("Signature", crypto.Base64Encode(hmac))
urlPath := h.API.Endpoints.URL + common.EncodeURLValues(endpoint, values)
var body []byte
if data != nil {
encoded, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("%s unable to marshal data: %s", h.Name, err)
return err
}
body = encoded
}
@@ -812,7 +776,7 @@ func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url
Path: urlPath,
Headers: headers,
Body: bytes.NewReader(body),
Result: result,
Result: &interim,
AuthRequest: true,
Verbose: h.Verbose,
HTTPDebugging: h.HTTPDebugging,

View File

@@ -1,20 +1,13 @@
package huobi
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/currency"
@@ -312,31 +305,6 @@ func TestCancelWithdraw(t *testing.T) {
}
}
func TestPEMLoadAndSign(t *testing.T) {
t.Parallel()
pemKey := strings.NewReader(h.API.Credentials.PEMKey)
pemBytes, err := ioutil.ReadAll(pemKey)
if err != nil {
t.Fatalf("TestPEMLoadAndSign Unable to ioutil.ReadAll PEM key: %s", err)
}
block, _ := pem.Decode(pemBytes)
if block == nil {
t.Fatalf("TestPEMLoadAndSign Block is nil")
}
x509Encoded := block.Bytes
privKey, err := x509.ParseECPrivateKey(x509Encoded)
if err != nil {
t.Fatalf("TestPEMLoadAndSign Unable to ParseECPrivKey: %s", err)
}
_, _, err = ecdsa.Sign(rand.Reader, privKey, crypto.GetSHA256([]byte("test")))
if err != nil {
t.Fatalf("TestPEMLoadAndSign Unable to sign: %s", err)
}
}
func setFeeBuilder() *exchange.FeeBuilder {
return &exchange.FeeBuilder{
Amount: 1,

View File

@@ -136,9 +136,6 @@ func (h *HUOBI) Setup(exch *config.ExchangeConfig) error {
return err
}
h.API.PEMKeySupport = exch.API.PEMKeySupport
h.API.Credentials.PEMKey = exch.API.Credentials.PEMKey
err = h.Websocket.Setup(
&wshandler.WebsocketSetup{
Enabled: exch.Features.Enabled.Websocket,

4
go.sum
View File

@@ -88,8 +88,6 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 h1:0IKlLyQ3Hs9nDaiK5cSHAGmcQ
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0/go.mod h1:mJzapYve32yjrKlk9GbyCZHuPgZsrbyIbyKhSzOpg6s=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.12.2 h1:D0EVSTwQoQOyfY35QNSuPJA4jpZRtkoGYWQMB7XNg5o=
github.com/grpc-ecosystem/grpc-gateway v1.12.2/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/grpc-ecosystem/grpc-gateway v1.13.0 h1:sBDQoHXrOlfPobnKw69FIKa1wg9qsLLvvQ/Y19WtFgI=
github.com/grpc-ecosystem/grpc-gateway v1.13.0/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
@@ -282,8 +280,6 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA=
google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

View File

@@ -1432,8 +1432,7 @@
},
"credentials": {
"key": "Key",
"secret": "Secret",
"pemKey": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIPVSj8YkpXibCAL9HwpGkDNSEXR9jcpiCthdikJqipNooAoGCCqGSM49\nAwEHoUQDQgAEHiB7q/HCqUrCNqPeTtRmKjyi2T+2O2JgoU8Mjx2R4z1h81uOZHCk\nxbsDg1fb7ACRMpKWPs59QWpQxhqMQrNw8w==\n-----END EC PRIVATE KEY-----\n"
"secret": "Secret"
},
"credentialsValidator": {
"requiresKey": true,