diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index f1f3c16a..91ab8584 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -26,6 +26,7 @@ type Provider struct { nodeURL string httpClient httpClient br breaker.Breaker + jwtToken string // optional chainID *big.Int // cache cachestore.Store[[]byte] // NOTE: unused for now @@ -90,6 +91,10 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { req = req.WithContext(ctx) req.Header.Set("Content-Type", "application/json") + if p.jwtToken != "" { + req.Header.Set("Authorization", fmt.Sprintf("BEARER %s", p.jwtToken)) + } + res, err := p.httpClient.Do(req) if err != nil { return nil, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to send request: %w", err)) @@ -101,7 +106,7 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { return nil, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to read resposne body: %w", err)) } - if res.StatusCode < 200 || res.StatusCode > 299 { + if (res.StatusCode < 200 || res.StatusCode > 299) && res.StatusCode != 401 { if len(body) > 100 { body = body[:100] } diff --git a/ethrpc/ethrpc_test.go b/ethrpc/ethrpc_test.go index 354d7bf5..1d5f6a39 100644 --- a/ethrpc/ethrpc_test.go +++ b/ethrpc/ethrpc_test.go @@ -193,3 +193,13 @@ func TestRaw(t *testing.T) { require.Empty(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()) +// } diff --git a/ethrpc/option.go b/ethrpc/option.go index 1945e0e9..90120bcf 100644 --- a/ethrpc/option.go +++ b/ethrpc/option.go @@ -36,3 +36,9 @@ func WithBreaker(br breaker.Breaker) Option { // p.cache = cache // } // } + +func WithJWTAuthorization(jwtToken string) Option { + return func(p *Provider) { + p.jwtToken = jwtToken + } +}