Skip to content

Commit

Permalink
ethproviders.NewProviders(): Pass ethrpc.Option, e.g. .WithHTTPClient…
Browse files Browse the repository at this point in the history
…() (#161)

* ethproviders.NewProviders(): Pass ethrpc.Option, e.g. .WithHTTPClient()

* Import transport/traceid pkgs used in tests
  • Loading branch information
VojtechVitek authored Mar 7, 2025
1 parent 1259dc2 commit 83b5ab7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
9 changes: 2 additions & 7 deletions ethproviders/ethproviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,19 @@ type ChainInfo struct {
Name string `json:"name"`
}

func NewProviders(cfg Config, optJwtToken ...string) (*Providers, error) {
func NewProviders(cfg Config, opts ...ethrpc.Option) (*Providers, error) {
providers := &Providers{
byID: map[uint64]*ethrpc.Provider{},
byName: map[string]*ethrpc.Provider{},
configByID: map[uint64]NetworkConfig{},
}

var providerJwtAuth ethrpc.Option
if len(optJwtToken) > 0 && optJwtToken[0] != "" {
providerJwtAuth = ethrpc.WithJWTAuthorization(optJwtToken[0])
}

for name, details := range cfg {
if details.Disabled {
continue
}

p, err := ethrpc.NewProvider(details.URL, providerJwtAuth)
p, err := ethrpc.NewProvider(details.URL, opts...)
if err != nil {
return nil, err
}
Expand Down
47 changes: 46 additions & 1 deletion ethproviders/ethproviders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package ethproviders_test

import (
"context"
"fmt"
"math/big"
"net/http"
"os"
"testing"

"github.com/0xsequence/ethkit/ethproviders"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/go-chi/traceid"
"github.com/go-chi/transport"
"github.com/stretchr/testify/require"
)

Expand All @@ -17,7 +23,46 @@ func TestBasic(t *testing.T) {
},
}

ps, err := ethproviders.NewProviders(cfg) //, "xx")
ps, err := ethproviders.NewProviders(cfg)
require.NoError(t, err)
p := ps.Get("polygon")
require.NotNil(t, p)

block, err := p.BlockByNumber(context.Background(), big.NewInt(1_000_000))
require.NoError(t, err)
require.NotNil(t, block)
require.Equal(t, uint64(1_000_000), block.NumberU64())
}

func TestClientWithJWTAuth(t *testing.T) {
// NODE_URL="https://dev-nodes.sequence.app"
// JWT_TOKEN=$(jwtutil -secret=changemenow -encode -claims='{"service":"test"}' 2>/dev/null)

nodeURL := os.Getenv("NODE_URL")
jwtToken := os.Getenv("JWT_TOKEN")

if jwtToken == "" || nodeURL == "" {
t.Skip("NODE_URL or JWT_TOKEN is not set")
}

cfg := ethproviders.Config{
"polygon": ethproviders.NetworkConfig{
ID: 137,
URL: fmt.Sprintf("%s/polygon", nodeURL),
},
}

httpClient := &http.Client{
Transport: transport.Chain(http.DefaultTransport,
traceid.Transport,
transport.SetHeaderFunc("Authorization", func(req *http.Request) string {
return "BEARER " + jwtToken
}),
transport.LogRequests(transport.LogOptions{Concise: true, CURL: true}),
),
}

ps, err := ethproviders.NewProviders(cfg, ethrpc.WithHTTPClient(httpClient))
require.NoError(t, err)
p := ps.Get("polygon")
require.NotNil(t, p)
Expand Down
10 changes: 0 additions & 10 deletions ethrpc/ethrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,6 @@ func TestDebugTraceTransaction(t *testing.T) {
require.NotEmpty(t, payload)
}*/

// func TestJWTAuth(t *testing.T) {
// p, err := ethrpc.NewProvider("https://dev-nodes.sequence.app/polygon", ethrpc.WithJWTAuthorization("xx"))
// require.NoError(t, err)

// block, err := p.BlockByNumber(context.Background(), big.NewInt(1_000_000))
// require.NoError(t, err)
// require.NotNil(t, block)
// require.Equal(t, uint64(1_000_000), block.NumberU64())
// }

func TestFetchBlockWithInvalidVRS(t *testing.T) {
url := "https://rpc.telos.net"
// url := "https://node.mainnet.etherlink.com"
Expand Down
6 changes: 0 additions & 6 deletions ethrpc/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ func WithBreaker(br breaker.Breaker) Option {
// }
// }

func WithJWTAuthorization(jwtToken string) Option {
return func(p *Provider) {
p.jwtToken = jwtToken
}
}

// 0: disabled, no validation (default)
// 1: semi-strict transactions – validates only transaction V, R, S values
// 2: strict block and transactions – validates block hash, sender address, and transaction signatures
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ require (
github.com/consensys/bavard v0.1.13 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-chi/traceid v0.2.0 // indirect
github.com/go-chi/transport v0.5.0 // indirect
github.com/goware/singleflight v0.2.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-chi/httpvcr v0.2.0 h1:jOsPvc4ZOoyNv9KCv/O4YoSjMFrHFq/Orc90A0DotUU=
github.com/go-chi/httpvcr v0.2.0/go.mod h1:tGX6IOmSd8LEvItVrT4z7I4BdhjHFU5RPTmvsKudD+Q=
github.com/go-chi/traceid v0.2.0 h1:M4SVlzbnq6zfNCOvi8LwLFGugY04El+hS8njO0Pwml4=
github.com/go-chi/traceid v0.2.0/go.mod h1:XFfEEYZjqgML4ySh+wYBU29eqJkc2um7oEzgIc63e74=
github.com/go-chi/transport v0.5.0 h1:xpnYcIOpBRrduJD68gX9YxkJouRGIE1y+rK5yGYnMXE=
github.com/go-chi/transport v0.5.0/go.mod h1:uoCleTaQiFtoatEiiqcXFZ5OxIp6s1DfGeVsCVbalT4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
Expand Down Expand Up @@ -175,6 +179,7 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit 83b5ab7

Please sign in to comment.