Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: connect over https #99

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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 @@
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 @@
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)
lucaslopezf marked this conversation as resolved.
Show resolved Hide resolved
u, err := url.Parse(tendermintRPC)
if err != nil {
return "", crgerrs.WrapError(crgerrs.ErrConfig, err.Error())
}

if u.Port() == "443" && u.Scheme != HTTPS {
lucaslopezf marked this conversation as resolved.
Show resolved Hide resolved
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
Loading