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

Commit

Permalink
Merge pull request #325 from threefoldtech/feat/horizon-url
Browse files Browse the repository at this point in the history
make horizon url configurable
  • Loading branch information
LeeSmet authored Jun 8, 2021
2 parents e68d9e0 + 6d4dbd1 commit 567be3d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmds/stellar/multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func create(c *cli.Context) error {
return err
}

wallet, err := stellar.New(seed, network, nil)
wallet, err := stellar.New(seed, network, nil, "")
if err != nil {
return err
}
Expand All @@ -53,7 +53,7 @@ func signAndSubmit(c *cli.Context) error {
network := c.String("network")
transaction := c.String("transaction")

wallet, err := stellar.New(seed, network, nil)
wallet, err := stellar.New(seed, network, nil, "")
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmds/tfexplorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func main() {
flag.BoolVar(&f.flushEscrows, "flush-escrows", false, "flush all escrows in the database, including currently active ones, and their associated addressses")
flag.BoolVar(&f.enablePProf, "pprof", false, "enable pprof")
flag.Int64Var(&f.prometheusPort, "prometheus-port", 3200, "port the run the prometheus server on")
flag.StringVar(&config.Config.HorizonURL, "horizon", "", "Horizon server URL to communicate with")

flag.Parse()

Expand Down Expand Up @@ -188,7 +189,7 @@ func createServer(f flags, client *mongo.Client, dropEscrowData bool) (*http.Ser
log.Fatal().Err(err).Msg("failed to create escrow database indexes")
}

wallet, err := stellar.New(f.seed, config.Config.WalletNetwork, f.backupSigners)
wallet, err := stellar.New(f.seed, config.Config.WalletNetwork, f.backupSigners, config.Config.HorizonURL)
if err != nil {
log.Fatal().Err(err).Msg("failed to create stellar wallet")
}
Expand Down
1 change: 1 addition & 0 deletions config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Settings struct {
WalletNetwork string
TFNetwork string
HorizonURL string
}

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/directory/types/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (f *Farm) Validate() error {
if config.Config.WalletNetwork != "" {
found := false
for _, a := range f.WalletAddresses {
validator, err := stellar.NewAddressValidator(config.Config.WalletNetwork, a.Asset)
validator, err := stellar.NewAddressValidator(config.Config.WalletNetwork, a.Asset, config.Config.HorizonURL)
if err != nil {
if errors.Is(err, stellar.ErrAssetCodeNotSupported) {
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/escrow/stellar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestPayoutDistribution(t *testing.T) {
assert.NoError(t, pd.Valid())
}

w, err := stellar.New("", stellar.NetworkTest, nil)
w, err := stellar.New("", stellar.NetworkTest, nil, "")
assert.NoError(t, err)

e := NewStellar(w, nil, "", gridnetworks.GridNetworkMainnet)
Expand Down
22 changes: 14 additions & 8 deletions pkg/stellar/stellar.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ type (
// stellarWallet is the foundation wallet
// Payments will be funded and fees will be taken with this wallet
stellarWallet struct {
keypair *keypair.Full
network string
assets map[Asset]struct{}
signers Signers
keypair *keypair.Full
network string
assets map[Asset]struct{}
signers Signers
horizonURL string
}

// Wallet interface
Expand Down Expand Up @@ -81,17 +82,18 @@ var (
// New stellar wallet from an optional seed. If no seed is given (i.e. empty string),
// the wallet will panic on all actions which need to be signed, or otherwise require
// a key to be loaded.
func New(seed, network string, signers []string) (Wallet, error) {
func New(seed, network string, signers []string, horizonURL string) (Wallet, error) {
assets := mainnetAssets

if len(signers) < 3 && seed != "" {
log.Warn().Msg("to enable escrow account recovery, provide at least 3 signers")
}

w := &stellarWallet{
network: network,
assets: assets,
signers: signers,
network: network,
assets: assets,
signers: signers,
horizonURL: horizonURL,
}

var err error
Expand Down Expand Up @@ -632,6 +634,10 @@ func (w *stellarWallet) keypairFromEncryptedSeed(seed string) (keypair.Full, err

// GetHorizonClient gets the horizon client based on the wallet's network
func (w *stellarWallet) GetHorizonClient() (*horizonclient.Client, error) {
if w.horizonURL != "" {
return &horizonclient.Client{HorizonURL: w.horizonURL}, nil
}

switch w.network {
case "testnet":
return horizonclient.DefaultTestNetClient, nil
Expand Down
15 changes: 10 additions & 5 deletions pkg/stellar/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import (

// AddressValidator validates stellar address
type AddressValidator struct {
network string
asset Asset
network string
asset Asset
horizonURL string
}

// NewAddressValidator creates an address validator instance
func NewAddressValidator(network, assetCode string) (*AddressValidator, error) {
w, err := New("", network, nil)
func NewAddressValidator(network, assetCode, horizonURL string) (*AddressValidator, error) {
w, err := New("", network, nil, "")
if err != nil {
return nil, errors.Wrap(err, "could not create wallet")
}
asset, err := w.AssetFromCode(assetCode)
if err != nil {
return nil, errors.Wrap(err, "could not load asset code")
}
return &AddressValidator{network: network, asset: asset}, nil
return &AddressValidator{network: network, asset: asset, horizonURL: horizonURL}, nil
}

// Valid validates a stellar address, and only return nil if address is valid
Expand Down Expand Up @@ -70,6 +71,10 @@ func (a *AddressValidator) getAccountDetails(address string) (account hProtocol.
}

func (a *AddressValidator) getHorizonClient() (*horizonclient.Client, error) {
if a.horizonURL != "" {
return &horizonclient.Client{HorizonURL: a.horizonURL}, nil
}

switch a.network {
case NetworkTest:
return horizonclient.DefaultTestNetClient, nil
Expand Down

0 comments on commit 567be3d

Please sign in to comment.