Skip to content

Commit 83b5ab7

Browse files
authored
ethproviders.NewProviders(): Pass ethrpc.Option, e.g. .WithHTTPClient() (#161)
* ethproviders.NewProviders(): Pass ethrpc.Option, e.g. .WithHTTPClient() * Import transport/traceid pkgs used in tests
1 parent 1259dc2 commit 83b5ab7

6 files changed

Lines changed: 55 additions & 24 deletions

File tree

ethproviders/ethproviders.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,19 @@ type ChainInfo struct {
2121
Name string `json:"name"`
2222
}
2323

24-
func NewProviders(cfg Config, optJwtToken ...string) (*Providers, error) {
24+
func NewProviders(cfg Config, opts ...ethrpc.Option) (*Providers, error) {
2525
providers := &Providers{
2626
byID: map[uint64]*ethrpc.Provider{},
2727
byName: map[string]*ethrpc.Provider{},
2828
configByID: map[uint64]NetworkConfig{},
2929
}
3030

31-
var providerJwtAuth ethrpc.Option
32-
if len(optJwtToken) > 0 && optJwtToken[0] != "" {
33-
providerJwtAuth = ethrpc.WithJWTAuthorization(optJwtToken[0])
34-
}
35-
3631
for name, details := range cfg {
3732
if details.Disabled {
3833
continue
3934
}
4035

41-
p, err := ethrpc.NewProvider(details.URL, providerJwtAuth)
36+
p, err := ethrpc.NewProvider(details.URL, opts...)
4237
if err != nil {
4338
return nil, err
4439
}

ethproviders/ethproviders_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package ethproviders_test
22

33
import (
44
"context"
5+
"fmt"
56
"math/big"
7+
"net/http"
8+
"os"
69
"testing"
710

811
"github.com/0xsequence/ethkit/ethproviders"
12+
"github.com/0xsequence/ethkit/ethrpc"
13+
"github.com/go-chi/traceid"
14+
"github.com/go-chi/transport"
915
"github.com/stretchr/testify/require"
1016
)
1117

@@ -17,7 +23,46 @@ func TestBasic(t *testing.T) {
1723
},
1824
}
1925

20-
ps, err := ethproviders.NewProviders(cfg) //, "xx")
26+
ps, err := ethproviders.NewProviders(cfg)
27+
require.NoError(t, err)
28+
p := ps.Get("polygon")
29+
require.NotNil(t, p)
30+
31+
block, err := p.BlockByNumber(context.Background(), big.NewInt(1_000_000))
32+
require.NoError(t, err)
33+
require.NotNil(t, block)
34+
require.Equal(t, uint64(1_000_000), block.NumberU64())
35+
}
36+
37+
func TestClientWithJWTAuth(t *testing.T) {
38+
// NODE_URL="https://dev-nodes.sequence.app"
39+
// JWT_TOKEN=$(jwtutil -secret=changemenow -encode -claims='{"service":"test"}' 2>/dev/null)
40+
41+
nodeURL := os.Getenv("NODE_URL")
42+
jwtToken := os.Getenv("JWT_TOKEN")
43+
44+
if jwtToken == "" || nodeURL == "" {
45+
t.Skip("NODE_URL or JWT_TOKEN is not set")
46+
}
47+
48+
cfg := ethproviders.Config{
49+
"polygon": ethproviders.NetworkConfig{
50+
ID: 137,
51+
URL: fmt.Sprintf("%s/polygon", nodeURL),
52+
},
53+
}
54+
55+
httpClient := &http.Client{
56+
Transport: transport.Chain(http.DefaultTransport,
57+
traceid.Transport,
58+
transport.SetHeaderFunc("Authorization", func(req *http.Request) string {
59+
return "BEARER " + jwtToken
60+
}),
61+
transport.LogRequests(transport.LogOptions{Concise: true, CURL: true}),
62+
),
63+
}
64+
65+
ps, err := ethproviders.NewProviders(cfg, ethrpc.WithHTTPClient(httpClient))
2166
require.NoError(t, err)
2267
p := ps.Get("polygon")
2368
require.NotNil(t, p)

ethrpc/ethrpc_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,6 @@ func TestDebugTraceTransaction(t *testing.T) {
296296
require.NotEmpty(t, payload)
297297
}*/
298298

299-
// func TestJWTAuth(t *testing.T) {
300-
// p, err := ethrpc.NewProvider("https://dev-nodes.sequence.app/polygon", ethrpc.WithJWTAuthorization("xx"))
301-
// require.NoError(t, err)
302-
303-
// block, err := p.BlockByNumber(context.Background(), big.NewInt(1_000_000))
304-
// require.NoError(t, err)
305-
// require.NotNil(t, block)
306-
// require.Equal(t, uint64(1_000_000), block.NumberU64())
307-
// }
308-
309299
func TestFetchBlockWithInvalidVRS(t *testing.T) {
310300
url := "https://rpc.telos.net"
311301
// url := "https://node.mainnet.etherlink.com"

ethrpc/option.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ func WithBreaker(br breaker.Breaker) Option {
4747
// }
4848
// }
4949

50-
func WithJWTAuthorization(jwtToken string) Option {
51-
return func(p *Provider) {
52-
p.jwtToken = jwtToken
53-
}
54-
}
55-
5650
// 0: disabled, no validation (default)
5751
// 1: semi-strict transactions – validates only transaction V, R, S values
5852
// 2: strict block and transactions – validates block hash, sender address, and transaction signatures

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ require (
5050
github.com/consensys/bavard v0.1.13 // indirect
5151
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
5252
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
53+
github.com/go-chi/traceid v0.2.0 // indirect
54+
github.com/go-chi/transport v0.5.0 // indirect
5355
github.com/goware/singleflight v0.2.0 // indirect
5456
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
5557
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
6666
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
6767
github.com/go-chi/httpvcr v0.2.0 h1:jOsPvc4ZOoyNv9KCv/O4YoSjMFrHFq/Orc90A0DotUU=
6868
github.com/go-chi/httpvcr v0.2.0/go.mod h1:tGX6IOmSd8LEvItVrT4z7I4BdhjHFU5RPTmvsKudD+Q=
69+
github.com/go-chi/traceid v0.2.0 h1:M4SVlzbnq6zfNCOvi8LwLFGugY04El+hS8njO0Pwml4=
70+
github.com/go-chi/traceid v0.2.0/go.mod h1:XFfEEYZjqgML4ySh+wYBU29eqJkc2um7oEzgIc63e74=
71+
github.com/go-chi/transport v0.5.0 h1:xpnYcIOpBRrduJD68gX9YxkJouRGIE1y+rK5yGYnMXE=
72+
github.com/go-chi/transport v0.5.0/go.mod h1:uoCleTaQiFtoatEiiqcXFZ5OxIp6s1DfGeVsCVbalT4=
6973
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
7074
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
7175
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
@@ -175,6 +179,7 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R
175179
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
176180
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
177181
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
182+
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
178183
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
179184
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
180185
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

0 commit comments

Comments
 (0)