From 47213f83125700cdd6721bb2dba8e0550ae13de1 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Wed, 8 Nov 2023 17:34:54 -0500 Subject: [PATCH] ethproviders: support jwtToken auth --- ethproviders/ethproviders.go | 9 +++++++-- ethproviders/ethproviders_test.go | 29 +++++++++++++++++++++++++++++ ethrpc/ethrpc.go | 3 +++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 ethproviders/ethproviders_test.go diff --git a/ethproviders/ethproviders.go b/ethproviders/ethproviders.go index 2cb9520f..2d88bb92 100644 --- a/ethproviders/ethproviders.go +++ b/ethproviders/ethproviders.go @@ -21,19 +21,24 @@ type ChainInfo struct { Name string `json:"name"` } -func NewProviders(cfg Config) (*Providers, error) { +func NewProviders(cfg Config, optJwtToken ...string) (*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) + p, err := ethrpc.NewProvider(details.URL, providerJwtAuth) if err != nil { return nil, err } diff --git a/ethproviders/ethproviders_test.go b/ethproviders/ethproviders_test.go new file mode 100644 index 00000000..0013e71c --- /dev/null +++ b/ethproviders/ethproviders_test.go @@ -0,0 +1,29 @@ +package ethproviders_test + +import ( + "context" + "math/big" + "testing" + + "github.com/0xsequence/ethkit/ethproviders" + "github.com/stretchr/testify/require" +) + +func TestBasic(t *testing.T) { + cfg := ethproviders.Config{ + "polygon": ethproviders.NetworkConfig{ + ID: 137, + URL: "https://dev-nodes.sequence.app/polygon", + }, + } + + ps, err := ethproviders.NewProviders(cfg) //, "xx") + 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()) +} diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 91ab8584..844506f5 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -39,6 +39,9 @@ func NewProvider(nodeURL string, options ...Option) (*Provider, error) { httpClient: http.DefaultClient, } for _, opt := range options { + if opt == nil { + continue + } opt(p) } return p, nil