Skip to content

Commit

Permalink
ethproviders: support jwtToken auth
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Nov 8, 2023
1 parent 927a4b4 commit 47213f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ethproviders/ethproviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions ethproviders/ethproviders_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
3 changes: 3 additions & 0 deletions ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47213f8

Please sign in to comment.