Skip to content

Commit

Permalink
fix: connect over https
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Mar 7, 2024
1 parent 5cfd4f4 commit f65df1c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
36 changes: 33 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package rosetta

import (
"fmt"
"strings"
"time"

crgerrs "github.com/cosmos/rosetta/lib/errors"

"github.com/coinbase/rosetta-sdk-go/types"
url "github.com/goware/urlx"
"github.com/spf13/pflag"

crg "github.com/cosmos/rosetta/lib/server"
Expand All @@ -18,6 +18,12 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
HTTP = "http"
HTTPS = "https"
TCP = "tcp"
)

// configuration defaults constants
const (
// DefaultBlockchain defines the default blockchain identifier name
Expand Down Expand Up @@ -150,13 +156,37 @@ func (c *Config) validate() error {
if c.TendermintRPC == "" {
return crgerrs.WrapError(crgerrs.ErrConfig, "cometbft rpc not provided")
}
if !strings.HasPrefix(c.TendermintRPC, "tcp://") {
c.TendermintRPC = fmt.Sprintf("tcp://%s", c.TendermintRPC)
validatedURL, err := c.validateUrl(c.TendermintRPC)
if err != nil {
return err
}
c.TendermintRPC = validatedURL

return nil
}

func (c *Config) validateUrl(tendermintRPC string) (string, error) {

Check failure on line 168 in config.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: method validateUrl should be validateURL (revive)
u, err := url.Parse(tendermintRPC)
if err != nil {
return "", crgerrs.WrapError(crgerrs.ErrConfig, err.Error())
}

if u.Port() == "443" && u.Scheme != HTTPS {
u.Scheme = HTTPS
}

if u.Port() == "" {
switch u.Scheme {
case HTTP, TCP:
u.Host += ":80"
case HTTPS:
u.Host += ":443"
}
}

return u.String(), nil
}

// WithCodec extends the configuration with a predefined Codec
func (c *Config) WithCodec(ir codectypes.InterfaceRegistry, cdc *codec.ProtoCodec) {
c.Codec = cdc
Expand Down
69 changes: 69 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package rosetta

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestConfig_validateUrl(t *testing.T) {
tests := []struct {
name string
tendermintRPC string
expected string
}{
{
name: "complete url",
tendermintRPC: "https://myhost.com:443",
expected: "https://myhost.com:443",
},
{
name: "no schema no port",
tendermintRPC: "myhost.com",
expected: "http://myhost.com:80",
},
{
name: "http schema with no port",
tendermintRPC: "http://myHost.com",
expected: "http://myhost.com:80",
},
{
name: "https schema with no port",
tendermintRPC: "https://myHost.com",
expected: "https://myhost.com:443",
},
{
name: "no schema with port",
tendermintRPC: "myHost.com:2344",
expected: "http://myhost.com:2344",
},
{
name: "no schema with port 443",
tendermintRPC: "myHost.com:443",
expected: "https://myhost.com:443",
},
{
name: "tcp schema",
tendermintRPC: "tcp://localhost:26657",
expected: "tcp://localhost:26657",
},
{
name: "localhost",
tendermintRPC: "localhost",
expected: "http://localhost:80",
},
{
name: "not normalized url",
tendermintRPC: "hTTp://tHISmyWebsite.COM",
expected: "http://thismywebsite.com:80",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{}
got, err := c.validateUrl(tt.tendermintRPC)
require.NoError(t, err)
require.Equal(t, tt.expected, got)
})
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/rosetta-sdk-go v0.10.0
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/goware/urlx v0.3.2
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
Expand All @@ -31,6 +32,8 @@ require (
github.com/99designs/keyring v1.2.1 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwS
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE=
Expand Down Expand Up @@ -334,6 +338,8 @@ github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo=
github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2/go.mod h1:EaizFBKfUKtMIF5iaDEhniwNedqGo9FuLFzppDr3uwI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down

0 comments on commit f65df1c

Please sign in to comment.