diff --git a/cmd/trade.go b/cmd/trade.go index 09391bd79..01f520c0a 100644 --- a/cmd/trade.go +++ b/cmd/trade.go @@ -507,7 +507,11 @@ func runTradeCmd(options inputs) { botConfig = convertDeprecatedBotConfigValues(l, botConfig) l.Infof("Trading %s:%s for %s:%s\n", botConfig.AssetCodeA, botConfig.IssuerA, botConfig.AssetCodeB, botConfig.IssuerB) - userID := "-1" // TODO DS Properly generate and save user ID. + userID, e := getUserID(l, botConfig) + if e != nil { + logger.Fatal(l, fmt.Errorf("could not get user id: %s", e)) + } + httpClient := &http.Client{} var guiVersionFlag string if *options.ui { @@ -730,6 +734,28 @@ func runTradeCmd(options inputs) { bot.Start() } +func getUserID(l logger.Logger, botConfig trader.BotConfig) (string, error) { + var userIDPrehash string + if botConfig.IsTradingSdex() { + userIDPrehash = botConfig.TradingAccount() + } else { + exchangeAPIKeys := botConfig.ExchangeAPIKeys.ToExchangeAPIKeys() + if len(exchangeAPIKeys) == 0 { + return "", fmt.Errorf("could not find exchange API key on bot config") + } + + userIDPrehash = exchangeAPIKeys[0].Key + } + + // hash avoids exposing the user account or api key + userIDHashed, e := utils.HashString(userIDPrehash) + if e != nil { + return "", fmt.Errorf("could not create user id: %s", e) + } + + return fmt.Sprint(userIDHashed), nil +} + func startMonitoringServer(l logger.Logger, botConfig trader.BotConfig) error { healthMetrics, e := monitoring.MakeMetricsRecorder(map[string]interface{}{"success": true}) if e != nil { diff --git a/support/sdk/ccxt.go b/support/sdk/ccxt.go index edccad809..d2f8e2954 100644 --- a/support/sdk/ccxt.go +++ b/support/sdk/ccxt.go @@ -3,7 +3,6 @@ package sdk import ( "encoding/json" "fmt" - "hash/fnv" "log" "net/http" "reflect" @@ -14,6 +13,7 @@ import ( "github.com/stellar/kelp/api" "github.com/stellar/kelp/support/networking" + "github.com/stellar/kelp/support/utils" ) // ccxtBaseURL should not have suffix of '/' @@ -182,22 +182,13 @@ func makeInstanceName(exchangeName string, apiKey api.ExchangeAPIKey) (string, e return exchangeName, nil } - number, e := hashString(apiKey.Key) + number, e := utils.HashString(apiKey.Key) if e != nil { return "", fmt.Errorf("could not hash apiKey.Key: %s", e) } return fmt.Sprintf("%s%d", exchangeName, number), nil } -func hashString(s string) (uint32, error) { - h := fnv.New32a() - _, e := h.Write([]byte(s)) - if e != nil { - return 0, fmt.Errorf("error while hashing string: %s", e) - } - return h.Sum32(), nil -} - func (c *Ccxt) hasInstance(instanceList []string) bool { for _, i := range instanceList { if i == c.instanceName { diff --git a/support/sdk/ccxt_test.go b/support/sdk/ccxt_test.go index dc43f5abc..1f46f3edd 100644 --- a/support/sdk/ccxt_test.go +++ b/support/sdk/ccxt_test.go @@ -10,6 +10,7 @@ import ( "github.com/stellar/kelp/api" "github.com/stellar/kelp/model" + "github.com/stellar/kelp/support/utils" ) func TestHashString(t *testing.T) { @@ -28,7 +29,7 @@ func TestHashString(t *testing.T) { for _, kase := range testCases { t.Run(kase.s, func(t *testing.T) { - result, e := hashString(kase.s) + result, e := utils.HashString(kase.s) if !assert.Nil(t, e) { return } diff --git a/support/utils/functions.go b/support/utils/functions.go index 27161663f..1f673948f 100644 --- a/support/utils/functions.go +++ b/support/utils/functions.go @@ -3,6 +3,7 @@ package utils import ( "encoding/json" "fmt" + "hash/fnv" "log" "math/big" "math/rand" @@ -418,3 +419,13 @@ func Offer2TxnBuildSellOffer(offer hProtocol.Offer) txnbuild.ManageSellOffer { OfferID: offer.ID, } } + +// HashString hashes a string using the FNV-1 hash function. +func HashString(s string) (uint32, error) { + h := fnv.New32a() + _, e := h.Write([]byte(s)) + if e != nil { + return 0, fmt.Errorf("error while hashing string: %s", e) + } + return h.Sum32(), nil +}