Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
When in testnet, do not allow secret keys that are valid accounts on …
Browse files Browse the repository at this point in the history
…the main network (closes #677) (#679)

* 1 - check on backend

* 2 - correct and clean up check on backend
  • Loading branch information
nikhilsaraf authored Mar 28, 2021
1 parent c370766 commit d52dea2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions gui/backend/upsert_bot_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ func (s *APIServer) validateConfigs(req upsertBotConfigRequest) *upsertBotConfig
errResp.TraderConfig.IssuerB = "cannot trade asset issued by trading account"
hasError = true
}
// use isTradingSdex() function because we have not called Iint() on the trader config yet
isPubnetAccount := req.TraderConfig.IsTradingSdex() && strings.TrimSuffix(req.TraderConfig.HorizonURL, "/") == strings.TrimSuffix(s.apiTestNet.HorizonURL, "/")
log.Printf("checking if secret key exists on pubnet: isTradingSdex=%v, isPubnetAccount=%v", req.TraderConfig.IsTradingSdex(), isPubnetAccount)
if isPubnetAccount {
// ensure that trader secret key does not exist on the main net
_, e := s.apiPubNet.AccountDetail(horizonclient.AccountRequest{AccountID: *tradingAccount})
if e != nil {
log.Printf("case: received error from call to check secret key on pubnet")
switch t := e.(type) {
case *horizonclient.Error:
if t.Problem.Status == 404 || strings.ToLower(t.Problem.Title) == "resource missing" {
log.Printf("case: account not found on pubnet error so it is valid case")
// this is the desired case
// do nothing
} else {
log.Printf("case: error from horizon while validating secret key being used on test network: status = %d, message = %s - %s", t.Problem.Status, t.Problem.Title, t.Problem.Detail)
errResp.TraderConfig.TradingSecretSeed = fmt.Sprintf("unknown error from Horizon (%d): %s", t.Problem.Status, t.Problem.Title)
hasError = true
}
default:
log.Printf("case: some other non-horizon error")
errResp.TraderConfig.TradingSecretSeed = fmt.Sprintf("unknown error checking key on pubnet: %s", e.Error())
hasError = true
}
} else {
log.Printf("case: account exists on pubnet, so this is an error!")
errResp.TraderConfig.TradingSecretSeed = "account for key exists on pubnet, cannot use on testnet"
hasError = true
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions trader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b *BotConfig) TradingPair() string {

// IsTradingSdex returns whether the config is set to trade on SDEX
func (b *BotConfig) IsTradingSdex() bool {
return b.isTradingSdex
return b.TradingExchange == "" || b.TradingExchange == "sdex"
}

// TradingExchangeName returns the defaulted trading exchange name
Expand All @@ -184,7 +184,7 @@ func (b *BotConfig) TradingExchangeName() string {

// Init initializes this config
func (b *BotConfig) Init() error {
b.isTradingSdex = b.TradingExchange == "" || b.TradingExchange == "sdex"
b.isTradingSdex = b.IsTradingSdex()

if b.AssetCodeA == b.AssetCodeB && b.IssuerA == b.IssuerB {
return fmt.Errorf("error: both assets cannot be the same '%s:%s'", b.AssetCodeA, b.IssuerA)
Expand Down

0 comments on commit d52dea2

Please sign in to comment.